We start with the same list we had in the chapter.
words <- c("Hello", "R")
for (word in words){
print(word)
}
Each item in the list gets printed on its own line:
Rscript loop.R
[1] "Hello"
[1] "R"
If we loop over a sequence:
words <- c("Hello", "R")
num_words = length(words)
for (i in seq(1,num_words)){
print(words[i])
}
Rscript loop.R
[1] "Hello"
[1] "R"
we see again that we have printed each word, but this time we have looped over the list indicies as a sequence.