intermediate - 0
# comparison
# equality
# following statements all evaluate to TRUE
3 == (2 + 1)
## [1] TRUE
"intermediate" != "r"
## [1] TRUE
TRUE != FALSE
## [1] TRUE
"Rchitect" != "rchitect" # R is case sensitive
## [1] TRUE
# Comparison of logicals
TRUE == FALSE
## [1] FALSE
# Comparison of numerics
-6 * 14 != 17 - 101
## [1] FALSE
# Comparison of character strings
"useR" == "user"
## [1] FALSE
# Compare a logical with a numeric
TRUE == 1
## [1] TRUE
# less than and greater than operators
# evaluate to FALSE
(1 + 2) > 4
## [1] FALSE
"dog" < "Cats" # greater than relationship based on alphabetical order
## [1] FALSE
TRUE <= FALSE
## [1] FALSE
# Comparison of numerics
-6 * 5 +2 >= -10 +1
## [1] FALSE
# Comparison of character strings
"raining" <= "raining dogs"
## [1] TRUE
# Comparison of logicals
TRUE > FALSE # TRUE corresponds to 1 in R, and FALSE coerces to 0 behind the scenes
## [1] TRUE
# Compare vectors
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)
# Popular days
linkedin > 15
## [1] TRUE FALSE FALSE FALSE FALSE TRUE FALSE
# Quiet days
linkedin <= 5
## [1] FALSE FALSE FALSE TRUE TRUE FALSE FALSE
# LinkedIn more popular than Facebook
linkedin > facebook
## [1] FALSE TRUE TRUE FALSE FALSE TRUE FALSE
# Compare matrices
views <- matrix(c(linkedin, facebook), nrow = 2, byrow = TRUE)
# When does views equal 13?
views == 13
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] FALSE FALSE TRUE FALSE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE FALSE TRUE FALSE
# When is views less than or equal to 14?
views <= 14
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] FALSE TRUE TRUE TRUE TRUE FALSE TRUE
## [2,] FALSE TRUE TRUE FALSE TRUE TRUE TRUE
# How often does facebook equal or exceed linkedin times two?
sum(facebook >= linkedin *2)
## [1] 2
# logical operators
# evaluate to TRUE:
TRUE & TRUE
## [1] TRUE
FALSE | TRUE
## [1] TRUE
5 <= 5 & 2 < 3
## [1] TRUE
3 < 4 | 7 < 6
## [1] TRUE
# to check if x is between 3 and 7
# 3 < x < 7 will NOT work
# 3 < x & x < 7
last <- tail(linkedin, 1)
# Is last under 5 or above 10?
last < 5 | last > 10
## [1] TRUE
# Is last between 15 (exclusive) and 20 (inclusive)?
last > 15 & last <= 20
## [1] FALSE
# linkedin exceeds 10 but facebook below 10
linkedin > 10 & facebook < 10
## [1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE
# When were one or both visited at least 12 times?
linkedin >= 12 | facebook >= 12 # at least means greater than or equal to
## [1] TRUE FALSE TRUE TRUE FALSE TRUE TRUE
# When is views between 11 (exclusive) and 14 (inclusive)?
views > 11 & views <= 14
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] FALSE FALSE TRUE FALSE FALSE FALSE TRUE
## [2,] FALSE FALSE FALSE FALSE FALSE TRUE TRUE
# conditonal statements
# if (condition) {
# expr
# }
medium <- "LinkedIn"
# information about the social website
num_views <- 14
# actual number of views that particular medium
# had on the last day of your recordings
# Examine the if statement for medium
if (medium == "LinkedIn") {
print("Showing LinkedIn information")
}
## [1] "Showing LinkedIn information"
# Write the if statement for num_views
if (num_views > 15) {
print("You're popular!")
}
# if (condition) {
# expr1
# } else { # same line as closing bracket of "if"
# expr2
# }
if (medium == "LinkedIn") {
print("Showing LinkedIn information")
} else {
print("Unknown medium")
}
## [1] "Showing LinkedIn information"
if (num_views > 15) {
print("You're popular!")
} else {
print("Try to be more visible!")
}
## [1] "Try to be more visible!"
# if (condition1) {
# expr1
# } else if (condition2) { # "else if" on same line
# as closing bracket of previous part
# expr2
# } else if (condition3) {
# expr3
# } else {
# expr4
# }
if (medium == "LinkedIn") {
print("Showing LinkedIn information")
} else if (medium == "Facebook") {
# Add code to print correct string when condition is TRUE
print("Showing Facebook information")
} else {
print("Unknown medium")
}
## [1] "Showing LinkedIn information"
if (num_views > 15) {
print("You're popular!")
} else if (num_views <= 15 & num_views > 10) {
# Add code to print correct string when condition is TRUE
print("Your number of views is average")
} else {
print("Try to be more visible!")
}
## [1] "Your number of views is average"
# while (condition) { # condition should become FALSE
# at some point
# during the execution
# expr
# }
# driver's assistant could
# give you different messages based on your speed
# or provide you with a current speed at a given moment
# Initialize the speed variable
speed <- 64
# Code the while loop
while ( speed > 30 ) {
print("Slow down!")
speed <- speed - 7
}
## [1] "Slow down!"
## [1] "Slow down!"
## [1] "Slow down!"
## [1] "Slow down!"
## [1] "Slow down!"
speed
## [1] 29
# Initialize the speed variable
speed <- 64
while (speed > 30) {
print(paste("Your speed is",speed))
if (speed > 48) {
print(paste("Slow down big time!"))
speed <- speed - 11
} else { print(paste("Slow down!"))
speed <- speed - 6
}
}
## [1] "Your speed is 64"
## [1] "Slow down big time!"
## [1] "Your speed is 53"
## [1] "Slow down big time!"
## [1] "Your speed is 42"
## [1] "Slow down!"
## [1] "Your speed is 36"
## [1] "Slow down!"
speed
## [1] 30
# the break statement is a control statement
speed <- 88
while (speed > 30) {
print(paste("Your speed is", speed))
# Break the while loop when speed exceeds 80
if (speed > 80 ) {
print(paste("Your speed is", speed,"> 80; thus break"))
break
}
if (speed > 48) {
print("Slow down big time!")
speed <- speed - 11
} else {
print("Slow down!")
speed <- speed - 6
}
}
## [1] "Your speed is 88"
## [1] "Your speed is 88 > 80; thus break"
# Initialize i as 1
i <- 1
# Code the while loop
while (i <= 10) {
print(3*i)
if ( (3*i) %% 8 == 0 ) {
break # if the triple of i is divisible by 8
}
i <- i + 1
}
## [1] 3
## [1] 6
## [1] 9
## [1] 12
## [1] 15
## [1] 18
## [1] 21
## [1] 24
# Loop over a vector
primes <- c(2, 3, 5, 7, 11, 13)
# loop version 1
for (p in primes) {
print(p)
}
## [1] 2
## [1] 3
## [1] 5
## [1] 7
## [1] 11
## [1] 13
# loop version 2
for (i in 1:length(primes)) {
print(primes[i])
}
## [1] 2
## [1] 3
## [1] 5
## [1] 7
## [1] 11
## [1] 13
# Loop over a list
primes_list <- list(2, 3, 5, 7, 11, 13)
# loop version 1
for (p in primes_list) {
print(p)
}
## [1] 2
## [1] 3
## [1] 5
## [1] 7
## [1] 11
## [1] 13
# loop version 2
for (i in 1:length(primes_list)) {
print(primes_list[[i]])
# need double square brackets - [[ ]]
# - to select the list elements
}
## [1] 2
## [1] 3
## [1] 5
## [1] 7
## [1] 11
## [1] 13
# The nyc list is already specified
nyc <- list(pop = 8405837,
boroughs = c("Manhattan", "Bronx", "Brooklyn", "Queens", "Staten Island"),
capital = FALSE)
# Loop version 1
for (l in nyc) {
print(l)
}
## [1] 8405837
## [1] "Manhattan" "Bronx" "Brooklyn" "Queens"
## [5] "Staten Island"
## [1] FALSE
# Loop version 2
for (i in 1:length(nyc)) {
print(nyc[[i]])
}
## [1] 8405837
## [1] "Manhattan" "Bronx" "Brooklyn" "Queens"
## [5] "Staten Island"
## [1] FALSE
# create matrix: ttt
# [,1] [,2] [,3]
# [1,] "O" NA "X"
# [2,] NA "O" "O"
# [3,] "X" NA "X"
row_1 <- c("O", NA, "X")
row_2 <- c(NA, "O", "O")
row_3 <- c("X", NA, "X")
ttt <- rbind(row_1, row_2, row_3)
# Loop over a matrix
for (i in 1:nrow(ttt)) {
for (j in 1:ncol(ttt)) {
print(paste("On row ", i, " and column ", j, " the board contains", ttt[i,j]))
}
}
## [1] "On row 1 and column 1 the board contains O"
## [1] "On row 1 and column 2 the board contains NA"
## [1] "On row 1 and column 3 the board contains X"
## [1] "On row 2 and column 1 the board contains NA"
## [1] "On row 2 and column 2 the board contains O"
## [1] "On row 2 and column 3 the board contains O"
## [1] "On row 3 and column 1 the board contains X"
## [1] "On row 3 and column 2 the board contains NA"
## [1] "On row 3 and column 3 the board contains X"
# use the if and else statements inside the for loop.
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
# Code the for loop with conditionals
for (li in linkedin) {
if (li > 10 ) {
print("You're popular!")
} else {
print("Be more visible!")
}
print(li)
}
## [1] "You're popular!"
## [1] 16
## [1] "Be more visible!"
## [1] 9
## [1] "You're popular!"
## [1] 13
## [1] "Be more visible!"
## [1] 5
## [1] "Be more visible!"
## [1] 2
## [1] "You're popular!"
## [1] 17
## [1] "You're popular!"
## [1] 14
# The break statement
# abandons the active loop:
# the remaining code in the loop is skipped
# and the loop is not iterated over anymore.
# The next statement
# skips the remainder of the code in the loop,
# but continues the iteration.
for (li in linkedin) {
if (li > 10) {
print("You're popular!")
} else {
print("Be more visible!")
}
# Add if statement with break
if (li > 16) {
print("This is ridiculous, I'm outta here!")
break
}
# Add if statement with next
if (li < 5) {
print("This is too embarrassing!")
next
}
}
## [1] "You're popular!"
## [1] "Be more visible!"
## [1] "You're popular!"
## [1] "Be more visible!"
## [1] "Be more visible!"
## [1] "This is too embarrassing!"
## [1] "You're popular!"
## [1] "This is ridiculous, I'm outta here!"
# count the number of r's that come before the first u in rquote?
# Pre-defined variables
rquote <- "r's internals are irrefutably intriguing"
chars <- strsplit(rquote, split = "")[[1]]
# Initialize rcount
rcount <- 0
# Finish the for loop
for (char in chars) {
if (char == "r") {
rcount <- rcount + 1
}
if (char == "u") {
break
}
}
# Print out rcount
rcount
## [1] 5