R can be tricky to wrap your head around at first.
You must follow pretty specific syntax rules for it to work. It won’t guess for you.
>
or in batch<-
is known as an “assignment operator” – it means “Make the object named to the left equal to the output of the code to the right”&
means AND, in Boolean logic|
means OR, in Boolean logic!
means NOT, in Boolean logic"United States"
, or "2016-07-26"
. Numbers are not quotedc
, for combine, with the values separated by commas, for exmaple: c:("2017-07-26", "2017-08-04")
c(1:10)
creates a list of integers (whole numbers) from one to ten.+
-
add, subtract*
/
multiply, divide>
<
greater than less than>=
<=
greater than or equal to, less than or equal to!=
not equal to==
tests whether the objects on either end are equal. This is often used in filtering data=
makes an object equal to a value, which is similar to <-
but used within a function.NA
is.na(x)
looks for nulls within variable x
.!is.na(x) looks for non-null values within variable
x`Here, is.na()
is a function. Functions are followed by parentheses, and act on code/data in the parentheses.
Object and variable names in R should not contain spaces
This is what happens when you run the two lines below in the console.
In those code sections, the code preceeded by ## is the output of the code from the lines above.
x <- c(4,4,5,6,7,2,9)
length(x); mean(x)
## [1] 7
## [1] 5.285714
plot(x)
It first stored the array for c(4,4,5,6,2,9)
into x
And the function length()
as applied to x
was length(x)
and the output was 7. This function counted the numbers in the array x
.
and then it was followed by the result of mean(x)
which was 5.285715. That is the average of all the numbers in the x
array.
And plot()
?
That charted out the results– the x-axis was the position of the number in the array and the y-axis was the actual number.
print()
, plot()
, and summary()
how to handle it.<-
operatorFunction | Action |
---|---|
getwd() |
List current working directory |
setwd("mydirectory") |
Change the current working directory to mydirectory |
ls() |
List the objects in the current workspace |
rm(object) |
Delete object |
save(objectlist, file="myfile) |
Save specific objects to a file |
load("myfile") |
Load a workspace into the current session (default = .RData) |
Mac users may need to alter their security preferences to allow apps authored by non-Apple developers to install. If you notice an error, try to change your system preferences.
Function | Action |
---|---|
help.start() |
General help |
help("foo") or ?foo |
Help on function foo (the quotation marks are optional) |
help.search("foo") or ??foo |
Search the help system for instances of the string foo |
example("foo") |
Examples of function foo (the quotation marks are optional) |
update.packages()
help(package=“packagename”)
search() #what packages are loaded
\
in a path name on Windows
setwd("c:\mydata")
will generate an error. Use setwd("c:/mydata")
or setwd("c:\\mydata")
insteadstr_trim()
is contained in the stringr package.