Reputation: 201
I'm working with R
and I have a code like this:
for (i in 1:10)
for (j in 1:100)
if (data[i] == paths[j,1])
cluster[i,4] <- paths[j,2]
where:
data
is a vector with 100 rows and 1 columnpaths
is a matrix with 100 rows and 5 columnscluster
is a matrix with 100 rows and 5 columnsMy question is: how could I avoid the use of "for" loops to iterate through the matrix? I don't know whether apply
functions (lapply
, tapply
...) are useful in this case.
This is a problem when j=10000
for example because the execution time is very long.
Thank you
Upvotes: 1
Views: 1258
Reputation: 30445
I think that both loops can be vectorized using the following:
cluster[na.omit(match(paths[1:100,1],data[1:10])),4] = paths[!is.na(match(paths[1:100,1],data[1:10])),2]
Upvotes: 1
Reputation: 50704
Inner loop could be vectorized
cluster[i,4] <- paths[max(which(data[i]==paths[,1])),2]
but check Musa's comment. I think you indented something else.
Second (outer) loop could be vectorize either, by replicating vectors but
i
is only 100 your speed-up don't be large[edit] As I understood your comment can you just use logical indexing?
indx <- data==paths[, 1]
cluster[indx, 4] <- paths[indx, 2]
Upvotes: 1