目前,我正在进行数据转换。数据不是很大,大约有19万行。如何在R中处理大型数据集时优化并加快循环?
我写了一个循环是这样的:
for (i in 1:nrow(df2)){
#a
record.a <- df[which(df$first_lat==df2[i,"third_lat"]
& df$first_lon==df2[i,"third_lon"]
& df$sixth_lat==df2[i,"fourth_lat"]
& df$sixth_lon==df2[i,"fourth_lon"]
& df[,4]==df2[i,4]
& df[,3]==df2[i,5]),]
df2[i,18] <- ifelse(nrow(record.a) != 0,record.a$order_cnt,NA)
#b
record.b <- df[which(df$fifth_lat==df2[i,"third_lat"]
& df$fifth_lon==df2[i,"third_lon"]
& df$sixth_lat==df2[i,"second_lat"]
& df$sixth_lon==df2[i,"second_lon"]
& df[,4]==df2[i,4]
& df[,3]==df2[i,5]),]
df2[i,19] <- ifelse(nrow(record.b) != 0,record.b$order_cnt,NA)
#c
record.c <- df[which(df$fifth_lat==df2[i,"first_lat"]
& df$fifth_lon==df2[i,"first_lon"]
& df$fourth_lat==df2[i,"second_lat"]
& df$fourth_lon==df2[i,"second_lon"]
& df[,4]==df2[i,4]
& df[,3]==df2[i,5]),]
df2[i,20] <- ifelse(nrow(record.c) != 0,record.c$order_cnt,NA)
#d
record.d <- df[which(df$third_lat==df2[i,"first_lat"]
& df$third_lon==df2[i,"first_lon"]
& df$fourth_lat==df2[i,"sixth_lat"]
& df$fourth_lon==df2[i,"sixth_lon"]
& df[,4]==df2[i,4]
& df[,3]==df2[i,5]),]
df2[i,21] <- ifelse(nrow(record.d) != 0,record.d$order_cnt,NA)
#e
record.e <- df[which(df$third_lat==df2[i,"fifth_lat"]
& df$third_lon==df2[i,"fifth_lon"]
& df$second_lat==df2[i,"sixth_lat"]
& df$second_lon==df2[i,"sixth_lon"]
& df[,4]==df2[i,4]
& df[,3]==df2[i,5]),]
df2[i,22] <- ifelse(nrow(record.e) != 0,record.e$order_cnt,NA)
#f
record.f <- df[which(df$first_lat==df2[i,"fifth_lat"]
& df$first_lon==df2[i,"fifth_lon"]
& df$second_lat==df2[i,"fourth_lat"]
& df$second_lon==df2[i,"fourth_lon"]
& df[,4]==df2[i,4]
& df[,3]==df2[i,5]),]
df2[i,23] <- ifelse(nrow(record.f) != 0,record.f$order_cnt,NA)
}
所以,基本上,我需要从DF 6个标准分别填写DF2的6列。在for循环中,nrow(df2)约为190k。它运行速度超慢。但我用查看(df2)来检查它,它运行良好。那么有什么方法可以让它更快?我可能会在未来将相同的数据转换应用于更大的数据集。
DF: df
DF2: df2
的数据是在地图上的网格。 df2基本上是df的一个子集,但增加了6个额外的列。 df和df2都有相同的lon和lat信息。
每个grid_id代表地图中的六边形区域。每个六边形通过两对lon和lat连接到其他六个六边形。我想要做的是从六个周围的六边形(以df)中找出一个特定值,填入df2中的列(a,b,c,d,e,f)。另外,我还需要其他两个条件,即几个小时,ten_mins_interval。 (DF [,4] == DF2 [I,4] & DF [3] == DF2 [I,5]))
因此,我认为逻辑是:
- 对于每个grid_id在DF2小时,ten_mins_interval(1行)
- 找到对应的6个grid_ids(6行)与相同小时,ten_mins_interval在DF从这些6行
- 填充order_cnt分为A,b,C,d,E,F df2中的列
您能提供一个可重复使用的小例子吗?在问题中粘贴输出'dput(head(df))'和'dput(head(df2 [,18:23]))''。 – Jimbou
for循环几乎总是不必要的,但您需要共享一些样本数据和预期结果,以便更容易理解您所需的内容。也许还可以简化问题 - 更少的列 –
目前您不太可能得到完整的答案,因为问题不可重现,即没有示例数据显示df和df2的结构。最可能的加速方法似乎是对6个块中的每个块使用“合并”函数来避免“for”循环 – Miff