Avoid the use of for loops

后端 未结 2 1695
迷失自我
迷失自我 2021-02-11 09:28

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] <         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-11 10:13

    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]
    

提交回复
热议问题