Loops Flashcards
1
Q
What is the recipe for a while loop in R?
A
while (cond) {
expression
}
2
Q
What statement exits a while or for loop?
A
break
3
Q
What statement skips the current iteration of a while or for loop?
A
next
4
Q
What is the recipe to loop over a vector or list in R using a for loop?
A
for (item in my_vector_or_list) {
expression
}
OR
for (i in 1:length(my_vector_or_list)) {
expression
}
5
Q
What is the recipe to loop over a matrix in R using two for loops?
A
for (i in 1:nrow(my_matrix)) {
for (j in 1:ncol(my_matrix)) {
expression
}
}