A call to print will always output the result of each iteration to the console. If you leave the print command, you may get unwanted output.
The main problem is that you did not initialize my_vector before the for loop. Instead, you can do the following and not get the side effect of print .
my_vector <- vector("numeric", 10L) for(i in 1:10) my_vector[i] <- i my_vector
But note that this is exactly the same as doing the following with sapply , but in this case you do not need an declaration, and you can assign the result to a vector.
my_vector2 <- sapply(1:10, function(x) x) my_vector2
source share