Principle and Syntax
Introduction
When developing computer code, we often need to iterate over elements stored in objects that are themselves stored in memory (e.g., vectors, matrices…). Depending on the problem, we might want to iterate over users, prices, products, chromosomes, genes… We have already seen that:
apply family functions are a solution for applying functions to the elements of an object.
Similarly, vectorization allows for implicitly iterating over objects to perform operations (e.g.,
x + 1
to iterate over all elements of a vector \(x\) and add1
to them).
R also offers the classic for loop structure found in many programming languages. The syntax of the for loop is given below.
for(element in my_object){
instruction 1
instruction 2
...
instruction n
}
element
is a variable whose name is freely chosen but should be explicit. The objectelement
will be instantiated (defined in memory) when the loop starts. Therefore, it does not need to be declared/created beforehand.my_object
is a pre-existing object. It must have been declared earlier in the code.my_object
must be an iterable object. It should be designed to be iterated over. This includes vectors, matrices, data.frames, lists, etc.
Exercices
- Given the following path (
tmp_path
), list (dir()
) all the files in this folder. Check the content of one of them withfile.show()
. - Using a for loop, read them with
scan()
(what="character"
,sep = "\n"
) and iteratively store their content (i.e., the object returned byscan()
) in a list namedmy_seq
.
tmp_path
my_seq <- list()
for(i in file.path(tmp_path, dir(tmp_path))){
my_seq[[basename(i)]] <- scan(file=i, what="character", sep = "\n")
}
End of the section
Thank you for following this tutorial.