Skip to content
Course Notes: Intermediate R
Intermediate R
Chapter 1: Conditionals and Control Flow
Relational Operators
Equality
TRUE == TRUE# returns TRUETRUE == FALSE# returns FALSE"hello" == "goodbye"# FALSE3 == 2# FALSE
Inequality !=
TRUE != TRUE# FALSETRUE != FALSE# returns TRUE"hello" != "goodbye"# TRUE3 != 2# TRUE
< and >
< and >3 < 5# TRUE3 > 5# FALSE
# Alphabetical Order! "Hello" > "Goodbye" # TRUE # TRUE coerces to 1 # FALSE coerces to 0 TRUE < FALSE # FALSE
<= and >=
<= and >=5 >= 3# TRUE3 >= 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 >= 5is.numeric(5)# TRUE!is.numeric(5)# FALSEis.numeric("hello")# FALSE!is.numeric("hello")# TRUE
Logical Operators and Vectors
c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)# TRUE FALSE FALSEc(TRUE, TRUE, FALSE) | c(TRUE, FALSE, FALSE)# TRUE TRUE FALSE!c(TRUE, TRUE, FALSE) # FALSE FALSE TRUE
& vs &&, | vs. ||
& vs &&, | vs. ||c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)# TRUE FALSE FALSEc(TRUE, TRUE, FALSE) && c(TRUE, FALSE, FALSE)# TRUEc(TRUE, TRUE, FALSE) | c(TRUE, FALSE, FALSE)# TRUE TRUE FALSEc(TRUE, TRUE, FALSE) || c(TRUE, FALSE, FALSE)# TRUE
Conditional Statements
if statement
if statementif(condition) { expr } x <- -3 if(x < 0) { print("x is a negative number") } # "x is a negative number"
else statement
else statementif(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
else if statementif(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
if, else if, elsex <- 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 loopwhile(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
while loopctr <- 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
break statementctr <- 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 loopfor(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
for loop over listcities <- 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
break statementfor(city in cities) { if(nchar(city) == 6) { break } print(city) } # "New York" # "Paris"
next statement
next statementfor (city in cities) { if(nchar(city) == 6) { next } print(city) } # "New York" # "Paris" # "Tokyo" # "Rio de Janeiro" # "Cape Town"
for loop: v2
for loop: v2for(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
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_sdFunction Documentation
help(sd)?sdArgument Matching
sd(x, na.rm = FALSE)
By Position
sd(values)
By name
sd(x = values)
na.rm argument
na.rm argumentvalues <- c(1, 5, 6, NA) sd(values) # returns NA
sd(values, TRUE)sd(values, na.rm = TRUE)Useful trick
args(sd)