Hey, I am struck on a problem that requires to a vector for the given condition. But as I've come across, I think the 'for loop' loops over from the beginning. The example will make it much clearer.
numbers <- vector("numeric",length=4)
for (i in c(2,7,9,12)){
numbers[[i]] <- i
print(i)
}
numbers
Output looks like:
What I want is to create a vector that looks something like:
[1] 2 7 9 11
You are using i as both the value to be stored in the vector and the index of the position in the vector. That is, you are storing 7 in the 7th position. A version the works is this:
numbers <- vector("numeric",length=4)
Vec <- c(2,7,9,12)
for (i in seq_along(numbers)){
numbers[i] <- Vec[i]
}
numbers
[1] 2 7 9 12