I have two data frames: logger and df (numbers are numeric):
logger <- data.frame( time = c(1280248354:1280248413), temp = runif(60,min=18,max=24.5) ) df <- data.frame( obs = c(1:10), time = runif(10,min=1280248354,max=1280248413), temp = NA )
I would like to search logger $ time for the closest match of each line in df $ time and assign the associated logger $ temp to df $ temp. So far, I have managed to use the following loop:
for (i in 1:length(df$time)){ closestto<-which.min(abs((logger$time) - (df$time[i]))) df$temp[i]<-logger$temp[closestto] }
However, now I have large data frames (logger has 13,620 lines, and df has 266138), and the processing time is long. I read that loops are not the most efficient way to do something, but I am not familiar with the alternatives. Is there a faster way to do this?
source share