我有两个非常大的数据框(50MM+ 行),我需要对它们进行一些计算。我开发了以下循环,但运行速度太慢。我尝试使用 apply 和其他方法,但我无法让它们工作。
#### Sample Data
df=data.frame(id=1:10,time=Sys.time()-1:10,within5=NA)
df2=data.frame(id2=c(1,1,1,5,5,10),time2=Sys.time()-c(9,5,2,3,4,6))
#### Loop shows how many results from df2 are within 5 secs of the creation of the ID in df
for (i in 1:length(df$id))
{
temp=df2[df2$id==df$id[i],]
df$within5[i]=sum(abs(as.numeric(difftime(temp$time2,df$time[i],units="secs")))<5)
}