Choices with if(), ifelse(), switch(), and case_when()
Looping with for, while, and repeat
Choices with if(), ifelse(), switch(), and case_when()
Looping with for, while, and repeat
Exercises
What is the basic form of an if() statement?
What is the basic form of an if() statement?
if (condition) true_actionif (condition) true_action else false_action
What is the basic form of an if() statement?
if (condition) true_actionif (condition) true_action else false_action
What are valid inputs for the condition?
What is the basic form of an if() statement?
if (condition) true_actionif (condition) true_action else false_action
What are valid inputs for the condition?
What is the basic form of an if() statement?
if (condition) true_actionif (condition) true_action else false_action
What are valid inputs for the condition?
For the action?
What is the basic form of an if() statement?
if (condition) true_actionif (condition) true_action else false_action
What are valid inputs for the condition?
For the action?
What is the basic form of an if() statement?
if (condition) true_actionif (condition) true_action else false_action
What are valid inputs for the condition?
For the action?
What does an if() statement return?
What is the basic form of an if() statement?
if (condition) true_actionif (condition) true_action else false_action
What are valid inputs for the condition?
For the action?
What does an if() statement return?
What is the difference between if() and ifelse()?
What is the difference between if() and ifelse()?
What is the difference between if() and ifelse()?
Is there another alternative to ifelse()?
What is the difference between if() and ifelse()?
Is there another alternative to ifelse()?
What is the difference between if() and ifelse()?
Is there another alternative to ifelse()?
Ok, so if() lets me create logical branches when the condition evaluates to FALSE. Is there a way I can get around nesting if statements?
What is the difference between if() and ifelse()?
Is there another alternative to ifelse()?
Ok, so if() lets me create logical branches when the condition evaluates to FALSE. Is there a way I can get around nesting if statements?
I really don't want to repeat this task over and over again. What can I do?
I really don't want to repeat this task over and over again. What can I do?
for (item in vector) perform_action
I really don't want to repeat this task over and over again. What can I do?
for (item in vector) perform_action
What happens to the value assigned to the item?
I really don't want to repeat this task over and over again. What can I do?
for (item in vector) perform_action
What happens to the value assigned to the item?
I really don't want to repeat this task over and over again. What can I do?
for (item in vector) perform_action
What happens to the value assigned to the item?
When should I create the container, and where?
I really don't want to repeat this task over and over again. What can I do?
for (item in vector) perform_action
What happens to the value assigned to the item?
When should I create the container, and where?
For loops use the index to determine the number of iterations. What if I don't know the number of iterations up front?
For loops use the index to determine the number of iterations. What if I don't know the number of iterations up front?
For loops use the index to determine the number of iterations. What if I don't know the number of iterations up front?
I get the purpose of a while loop. But what about a repeat loop?
For loops use the index to determine the number of iterations. What if I don't know the number of iterations up front?
I get the purpose of a while loop. But what about a repeat loop?
For loops use the index to determine the number of iterations. What if I don't know the number of iterations up front?
I get the purpose of a while loop. But what about a repeat loop?
Apparently it is powerful though. To quote from the book:
You can rewrite any for loop to use while instead, and you can rewrite any while loop to use repeat, but the converses are not true. That means while is more flexible than for, and repeat is more flexible than while. It’s good practice, however, to use the least-flexible solution to a problem, so you should use for wherever possible.
What type of vector does each of the following calls to ifelse() return?
ifelse(TRUE, 1, "no")ifelse(FALSE, 1, "no")ifelse(NA, 1, "no")
What type of vector does each of the following calls to ifelse() return?
ifelse(TRUE, 1, "no")ifelse(FALSE, 1, "no")ifelse(NA, 1, "no")
The "choice" is evaluated to true, false, and unknown, respectively. The return values are mixed between numeric and character values, while unknown is returned for an unknown evaluation.
Why does the following code work?
x <- 1:10if (length(x)) "not empty" else "empty"#> [1] "not empty"x <- numeric()if (length(x)) "not empty" else "empty"#> [1] "empty"
Why does the following code work?
x <- 1:10if (length(x)) "not empty" else "empty"#> [1] "not empty"x <- numeric()if (length(x)) "not empty" else "empty"#> [1] "empty"
0 and 1 represent FALSE and TRUE. The length function in the first case returns a positive integer, while in the second case return a 0. 0 is interpreted as FALSE, while 1+ is interpreted as TRUE.
Why does this code succeed without errors or warnings?
x <- numeric()out <- vector("list", length(x))for (i in 1:length(x)) { out[i] <- x[i] ^ 2}out
Why does this code succeed without errors or warnings?
x <- numeric()out <- vector("list", length(x))for (i in 1:length(x)) { out[i] <- x[i] ^ 2}out
Iteration on a zero length vector returns an unknown value.
When the following code is evaluated, what can you say about the vector being iterated?
x <- c(1, 2, 3)xs <- c(1, 2, 3)for (x in xs) { xs <- c(xs, x * 2)}xs
When the following code is evaluated, what can you say about the vector being iterated?
x <- c(1, 2, 3)xs <- c(1, 2, 3)for (x in xs) { xs <- c(xs, x * 2)}xs
This is replication a vertorized operation using a for loop. It concatenates the string, and each element * 2.
What does the following code tell you about when the index is updated?
for (i in 1:3) { i <- i * 2 print(i) }#> [1] 2#> [1] 4#> [1] 6
What does the following code tell you about when the index is updated?
for (i in 1:3) { i <- i * 2 print(i) }#> [1] 2#> [1] 4#> [1] 6
This assigns the value to i, multiplies it by 2, and then prints the value assigned to i. It repeats this for each element in the vector. At the end, you see all values printed once, while i points to the last value it was assigned during iteration, 6.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |