用r中的seq加速嵌套for循环

时间:2017-01-11 14:39:26

标签: r performance for-loop

我想用二进制值创建一个新列(df_one $ acceslane)。 如果df_one$direction == df_two$direction以及df_one$locationdf_two$location几乎相同( - >参见嵌套for循环中的距离),则应为1。

df_one:

direction | location | acceslane    
L | 12.3 | NA
R | 14.8 | NA

df_two:

direction | location
L | 12.5 
R | 145.0

for (i in 1:nrow(df_one)) {
  for (j in 1:nrow(df_two)) {
    Distance <- seq(df_two[j, 2]-.5, df_two[j, 2]+.5, by = .1)
    if ((x[i, 1] == df_two[j, 1]) & (x[i, 2] %in% Distance)){
      df_one[i, 3] <- 1
      break}
    else{df_one[i, 3] <- 0}
  }
}

所以这段代码可行,但速度不是很快。我怎样才能加快速度呢?

1 个答案:

答案 0 :(得分:5)

您的示例不适用于我,但我认为您正在寻求滚动加入:

library(data.table)

df_one <- fread("direction | location     
             L | 12.3 
             L | 12.7 
             L | 13.1 
             R | 14.8 ", sep = "|")
df_two <- fread("direction | location
             L | 12.5 
             R | 145.0", sep = "|")

df_one[, acceslane := 0]
df_one[df_two, acceslane := 1, on = .(direction, location), roll = 0.5]
df_one[df_two, acceslane := 1, on = .(direction, location), roll = -0.5]
#   direction location acceslane
#1:         L     12.3         1
#2:         L     12.7         1
#3:         L     13.1         0
#4:         R     14.8         0

PS:永远不要依赖十进制数字的精确比较,否则您迟早会问FAQ