I would like to iteratively add elements to a list over which I loop:
list = as.list(c(1,2,3))
list
for (x in list) {
new_element = as.list(x^2)
print(new_element)
list = union(list, new_element)
}
list
However, R takes only the original 3 elements in the loop. I would like that the loop continues with the new elements. Any ideas on how I could adjust my code? Thanks!
if(x < 10){...}as the body of the loop, along with an explicit intended output ((1,2,3,4,9,16,81)? )