x <- 1 # attribute the value '1' to the variable 'x'
x # print the value of 'x'
#> [1] 1
In R, the variable attribution is done through the arrow operator <-
instead of the =
one – for historical reasons. The equal sign, =
, would still work though, but you should just take up the habit of using <-
.
In reality, scalar numbers don’t exist in R, they are simply 1 element vector: this is why #> [1]
is printed in the following outputs.
To understand what you will encounter further down this class, inputs (what you type in the R script and hit Ctrl+⏎ (⌘+⏎)) are the shaded areas with syntax coloring, while outputs (what R returns) are the area underneath starting with #> [1]
. In case the output spans several lines, each line would start with #> [x]
where x
is the index of the first element printed on the line.
Note that different instructions are usually given on separated lines (i.e. one instruction per line), but several instructions can be written on a single line if separated with a ;
sign.
In R code, what is written after the #
sign is a comment and is thus not interpreted.
Defining a scalar value is done with:
x <- 1 # attribute the value '1' to the variable 'x'
x # print the value of 'x'
#> [1] 1
You can use the R console as an advanced calculator:
Typing x + 1
will only print the result of x + 1
, but not add 1 to x
and save this as the new value of x
.
To actually modify the value stored in x
, type x <- x + 1
.
R handles infinity, NaN (Not a Number), and has \(\pi\) defined. Missing numbers are handled through the NA
keyword (Not Attributed).
pi
#> [1] 3.141593
10/0
#> [1] Inf
0/0
#> [1] NaN
NA
#> [1] NA
Booleans are handled with the TRUE
and FALSE
keywords, and are usually obtained as the result of a test on values.
Once you’ve understood these tests, you can use them in conditional actions of the type if(test){then}else{then}
, or to apply some filter on your data.
Let’s create two variables x
and y
and test some assertions on these two variables (i.e. answer the following questions):
x <- 1
y <- 2
x
equal to y
?x == y
#> [1] FALSE
x
not equal to y
?x != y
#> [1] TRUE
x
smaller than y
?x < y
#> [1] TRUE
x
smaller or equal than y
?x <= y
#> [1] TRUE
&
x == y & x < y
#> [1] FALSE
|
x == y | x < y
#> [1] TRUE
x
a NaN?is.nan(x)
#> [1] FALSE
x
a number?is.numeric(x)
#> [1] TRUE
x
a string?is.character(x)
#> [1] FALSE
x
a NA?is.na(x)
#> [1] FALSE
!is.na(x)
#> [1] TRUE
A number can by converted to a boolean using as.logical()
: any number non 0 is equivalent to TRUE
, and 0 is FALSE
.
R also natively handles complex values:
A string is defined between quotation marks such as: "string"
. Thus "1"
is not the number 1, but rather the character “1”. Here are some operations on strings :
phrase <- "Hello World "
paste()
:paste("phrase", phrase, sep=" = ")
#> [1] "phrase = Hello World "
glue::glue()
:substr(phrase, 1, 4)
#> [1] "Hell"
tolower()
and toupper()
:sub()
:sub("o", "a", phrase)
#> [1] "Hella World "
gsub()
:gsub("o", "a", phrase)
#> [1] "Hella Warld "
trimws()
:trimws(phrase)
#> [1] "Hello World"
strsplit()
:phrase2 <- "1234"
phrase2 - 4321 # won't work: string - double
#> Error in phrase2 - 4321: non-numeric argument to binary operator
as.numeric(phrase2) - 4321 # conversion of string to double
#> [1] -3087
For more complex operations, see the stringr
package and its cheatsheet.
Download the exercises and solutions from the following repository, then create a Rstudio project from the unzipped folder: