I want to create a new column (df_one$acceslane) with binary values.
if df_one$direction == df_two$direction
and if df_one$location
and df_two$location
are almost the same (-> see Distance in the nested for loop) it should be a 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}
}
}
So this code works, but it's not very fast. How can I speed this up?