Let’s initialize a new vector called v
:
v<-c()
We can append new elements in a for-loop using the concatenate function c
. For this example, we will iterate from 1 to 10 and we will take the power of two of each element, and we will append it to our vector v
:
v<-c() for (i in 1:10) { tmp<-i**2 v<-c(v,tmp) } print(v)
[1] 1 4 9 16 25 36 49 64 81 100