Skip to content
New Workbook
Sign up
Course Notes: Intermediate R

Intermediate R

Chapter 1: Conditionals and Control Flow

Relational Operators

Equality

  • TRUE == TRUE # returns TRUE
  • TRUE == FALSE # returns FALSE
  • "hello" == "goodbye" # FALSE
  • 3 == 2 # FALSE

Inequality !=

  • TRUE != TRUE # FALSE
  • TRUE != FALSE # returns TRUE
  • "hello" != "goodbye" # TRUE
  • 3 != 2 # TRUE

< and >

  • 3 < 5 # TRUE
  • 3 > 5 # FALSE
# Alphabetical Order! "Hello" > "Goodbye" # TRUE # TRUE coerces to 1 # FALSE coerces to 0 TRUE < FALSE # FALSE

<= and >=

  • 5 >= 3 # TRUE
  • 3 >= 3 # TRUE

Relational Operators & Vectors

linkedin <- c(16, 9, 13, 5, 2, 17, 14) linkedin # 16, 9, 13, 5, 2, 17, 14 linkedin > 10 # TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE

Logical Operators

  • AND &
  • OR |
  • NOT !

AND operator &

  • TRUE & TRUE # TRUE
  • FALSE & TRUE # FALSE
  • TRUE & FALSE # FALSE
  • FALSE & FALSE # FALSE
x <- 12 x > 5 & x < 15 # TRUE x <- 17 x > 5 & x < 15 # FALSE

OR operator |

y <- 4 y < 5 | y > 15 # TRUE y <- 14 y < 5 | y > 15 # FALSE

NOT operator !

  • !TRUE # FALSE
  • !FALSE # TRUE
  • !(X<5) # x >= 5
  • is.numeric(5) # TRUE
  • !is.numeric(5) # FALSE
  • is.numeric("hello") # FALSE
  • !is.numeric("hello") # TRUE

Logical Operators and Vectors

  • c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE) # TRUE FALSE FALSE
  • c(TRUE, TRUE, FALSE) | c(TRUE, FALSE, FALSE) # TRUE TRUE FALSE
  • !c(TRUE, TRUE, FALSE) # FALSE FALSE TRUE

& vs &&, | vs. ||

  • c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE) # TRUE FALSE FALSE
  • c(TRUE, TRUE, FALSE) && c(TRUE, FALSE, FALSE) # TRUE
  • c(TRUE, TRUE, FALSE) | c(TRUE, FALSE, FALSE) # TRUE TRUE FALSE
  • c(TRUE, TRUE, FALSE) || c(TRUE, FALSE, FALSE) # TRUE

Conditional Statements

if statement

if(condition) { expr } x <- -3 if(x < 0) { print("x is a negative number") } # "x is a negative number"

else statement

if(condition) { expr1 } else { expr2 } x <- 3 if(x < 0) { print("x is a negative number") } else { print("x is either a positive number or zero") } # "x is a negative number" x <- 5 if(x < 0) { print("x is a negative number") } else { print("x is either a positive number or zero") } # "x is either a positive number or zero"

else if statement

if(condition1) { expr1 } elseif(condition2) { expr2 } else { expr3 } x <- 3 if(x < 0) { print("x is a negative number") } elseif(x == 0) { print("x is zero") } else { print("x is a positive number") } # "x is a negative number" x <- 0 if(x < 0) { print("x is a negative number") } elseif(x == 0) { print("x is zero") } else { print("x is a positive number") } # "x is a zero" x <- 5 if(x < 0) { print("x is a negative number") } elseif(x == 0) { print("x is zero") } else { print("x is a positive number") } # "x is a positive number"

if, else if, else

x <- 6 if(x %% 2 == 0) { print("divisible by 2") } elseif(x %% 3 == 0) { print("divisible by 3") } else { print("not divisible by 2 nor by 3...") } # "divisible by 2"

Chapter 2: Loops

While loop

while(condition) { expr } while(ctr <= 7) { print(paste("ctr is set to", ctr)) ctr <- ctr + 1 } # "ctr is set to 1" while(ctr <= 7) { print(paste("ctr is set to", ctr)) ctr <- ctr + 1 } # "ctr is set to 2" while(ctr <= 7) { print(paste("ctr is set to", ctr)) ctr <- ctr + 1 } # "ctr is set to 7" ctr <- 1 while(ctr <= 7) { print(paste("ctr is set to", ctr)) ctr <- ctr + 1 } # "ctr is set to 1""ctr is set to 2"..."ctr is set to 7" ctr # 8

Infinite while loop

ctr <- 1 while(ctr <= 7) { print(paste("ctr is set to", ctr)) } # "ctr is set to 1""ctr is set to 1""ctr is set to 1""ctr is set to 1""ctr is set to 1""ctr is set # to 1""ctr is set to 1"...

break statement

ctr <- 1 while(ctr <= 7) { if(ctr %% 5 == 0) { break } print(paste("ctr is set to", ctr)) ctr <- ctr + 1 } # "ctr is set to 1" # "ctr is set to 2" # "ctr is set to 3" # "ctr is set to 4"

for loop

for(var in seq) { expr } cities <- c("New York", "Paris", "London", "Tokyo", "Rio de Janeiro", "Cape Town") cities # "New York" "Paris" ... "Cape Town" for(city in cities) { print(city) } # "New York"

for loop over list

cities <- list("New York", "Paris", "London", "Tokyo","Rio de Janeiro", "Cape Town") for(city in cities) { print(city) } # "New York" # "Paris" # "London" # "Tokyo" # "Rio de Janeiro" # "Cape Town"

break statement

for(city in cities) { if(nchar(city) == 6) { break } print(city) } # "New York" # "Paris"

next statement

for (city in cities) { if(nchar(city) == 6) { next } print(city) } # "New York" # "Paris" # "Tokyo" # "Rio de Janeiro" # "Cape Town"

for loop: v2

for(i in 1:length(cities)) { print(cities[i], "is on position" i, "in the cities vector.") } # "New York is on position 1 in the cities vector." # "Paris is on position 2 in the cities vector." # "London is on position 3 in the cities vector." # "Tokyo is on position 4 in the cities vector." # "Rio de Janeiro is on position 5 in the cities vector." # "Cape Town is on position 6 in the cities vector."

for loop: wrap-up

# Concise, easy to read, but no access to looping index for(city in cities) { print(city) } #Harder to read and write, but more versatile for(i in 1:length(cities)) { print(cities[i], "is on position", i, "in the cities vector.")) }

Chapter 3: Functions

  • You already know 'em!
  • Create a list: list()
  • Display a variable: print()
sd(c(1, 5, 6, 7))
values <- c(1, 5, 6, 7)
sd(values)
my_sd <- sd(values)
my_sd

Function Documentation

help(sd)
?sd

Argument Matching

sd(x, na.rm = FALSE)
By Position
sd(values)
By name
sd(x = values)

na.rm argument

values <- c(1, 5, 6, NA) sd(values) # returns NA
sd(values, TRUE)
sd(values, na.rm = TRUE)

Useful trick

args(sd)