Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

Fast and efficient way to loop below code in R

I want to run the below loop in an efficient way as I need to perform this on millions of rows. Sample data

a <- data.frame(x1=rep(c('a','b','c','d'),5),
                x2=c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5),
                value1=c(rep(201,4),rep(202,4),rep(203,4),rep(204,4),rep(205,4)),
                y1=c(rep('a',4),rep('b',4),rep('c',4),rep('d',4),rep('e',4)),
                y2=c(1,2,3,4,2,3,4,5,3,4,5,6,4,5,6,7,5,6,7,8),
                value2=seq(101,120), stringsAsFactors = FALSE)

I wrote below to compare similar values between two columns and then find the difference.

for (i in 1:length(a$x1)){
  for (j in 1:length(a$x1)){
    if(a$y1[i] == a$x1[j] & a$y2[i] == a$x2[j]){
      a$diff[i] <- a$value1[j] - a$value2[i]
      break
    }
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For each i, you are to find the first j such that a$y1[i] == a$x1[j] && a$y2[i] == a$x2[j] (in your code, there is & instead of && which is obviously wrong).

If a$x1, a$x2, a$y1, a$y2 are either numbers or character data without spaces (like in your example), you could use

x12 = paste(a$x1, a$x2)
y12 = paste(a$y1, a$y2) 

then for each i, you are to find the first j such that x12[i]==y12[j]

You do it with match(x12, y12).

So you can do something like this:

x12 = paste(a$x1, a$x2)
y12 = paste(a$y1, a$y2) 
m = match(x12, y12)
for (i in seql(m))
    if (!is.na(m[i]))
        a$diff[i] <- a$value1[m[i]] - a$value2[i]

You can eliminate the last loop like this:

x12 = paste(a$x1, a$x2)
y12 = paste(a$y1, a$y2) 
m = match(x12, y12)
good.i = which(!is.na(m))
a$diff[good.i] <- a$value1[m[good.i]] - a$value2[good.i]   

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...