我想用二进制值创建一个新列(df_one $ acceslane).if df_one$direction == df_two$direction
和if df_one$location
和df_two$location
几乎相同( - >参见嵌套for循环中的Distance)它应该是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} } }
所以这段代码可行,但速度不是很快.我怎样才能加快速度呢?
您的示例不适用于我,但我认为您正在寻求滚动连接:
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.