I am running a loop in R to find indices of a vector when its elements are equal to elements of a reference vector.
As far as I know R, I need to declare the variable before the for-loop, but in this case I do not know the final length of my indices vector (see code below).
How can I create a variables that allows R to change its size during the for loop?
extract of my code:
k <- 1
for(i in 1:length(Lid.time)){
ind <- which(Net.time==Lid.time[i])
if(length(ind)>0){
ind.Net[k] <- ind
k <- k+1
}
}
Notes about the code:
Lid.time is a vector of a different lenght than Net.time.
I need to find an array of indices that tells me where Net.time is equal to Lid.time. I do not know in advance how long will the ind.Net vector will be, so how can I declare the vector ind.Net?
Thanks for your help
As Dason stated,
matchwill work just fine for that specific task:But to answer your question of a vector of unknown length within a loop:
It will be different every time you run it because of
sample.