4  Variables, booleans and strings

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

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.

4.1 Scalars and booleans

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:

1 + 3
#> [1] 4
x <- 1/2
y <- exp(log(sin(cos(x*pi))))
x - y
#> [1] 0.5
Attention

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.

4.1.1 Special values

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

4.1.2 Booleans and common tests on values

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
  • Is x equal to y?
x == y
#> [1] FALSE
  • Is x not equal to y?
x != y
#> [1] TRUE
  • Is x smaller than y?
x < y 
#> [1] TRUE
  • Is x smaller or equal than y?
x <= y
#> [1] TRUE
  • Operator “and”: &
x == y & x < y 
#> [1] FALSE
  • Operator “or”: |
x == y | x < y 
#> [1] TRUE
  • Is x a NaN?
#> [1] FALSE
  • Is x a number?
#> [1] TRUE
  • Is x a string?
#> [1] FALSE
  • Is x a NA?
#> [1] FALSE
  • Operator “not” (inverse)
!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.

#> [1] TRUE
#> [1] FALSE

4.2 Complex values

R also natively handles complex values:

1+i   # not a valid complex value
#> Error in eval(expr, envir, enclos): object 'i' not found
1+1i  # valid complex value
#> [1] 1+1i
exp(1i*pi)
#> [1] -1+0i
sqrt(-1)
#> [1] NaN
sqrt(-1 + 0i)
#> [1] 0+1i
Im(exp(1i*pi))
#> [1] 1.224647e-16
Re(exp(1i*pi))
#> [1] -1
Mod(exp(1i*pi))
#> [1] 1

4.3 Strings

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 :

  • Definition of a string:
phrase <- "Hello World " 
  • Concatenation of strings using paste():
paste("phrase", phrase, sep=" = ")
#> [1] "phrase = Hello World "
  • Concatenation of strings using glue::glue():
library(glue)
glue("phrase = {phrase}")
#> phrase = Hello World
  • Acessing a sub-string:
substr(phrase, 1, 4)
#> [1] "Hell"
  • Change case of string with tolower() and toupper():
tolower(phrase); toupper(phrase)
#> [1] "hello world "
#> [1] "HELLO WORLD "
  • Change the first occurrence of “o” in “a” with sub():
sub("o", "a", phrase)
#> [1] "Hella World "
  • Change all occurrences of “o” in “a” with gsub():
gsub("o", "a", phrase)
#> [1] "Hella Warld "
  • Trim white spaces with trimws():
trimws(phrase)
#> [1] "Hello World"
  • Get a vector from string separation based on a character with strsplit():
strsplit(phrase, " ")         # returns a list
#> [[1]]
#> [1] "Hello" "World"
unlist(strsplit(phrase, " ")) # returns a vector
#> [1] "Hello" "World"
Attention!!
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.

4.4 Exercises

Download the archive with all the exercises files, unzip it in your R class RStudio project, and edit the R files.