2017-06-07 69 views
0

目前,我正在进行数据转换。数据不是很大,大约有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]))

因此,我认为逻辑是:

  1. 对于每个grid_id在DF2小时,ten_mins_interval(1行)
  2. 找到对应的6个grid_ids(6行)与相同小时,ten_mins_interval在DF从这些6行
  3. 填充order_cnt分为A,b,C,d,E,F df2中的列
+0

您能提供一个可重复使用的小例子吗?在问题中粘贴输出'dput(head(df))'和'dput(head(df2 [,18:23]))''。 – Jimbou

+0

for循环几乎总是不必要的,但您需要共享一些样本数据和预期结果,以便更容易理解您所需的内容。也许还可以简化问题 - 更少的列 –

+0

目前您不太可能得到完整的答案,因为问题不可重现,即没有示例数据显示df和df2的结构。最可能的加速方法似乎是对6个块中的每个块使用“合并”函数来避免“for”循环 – Miff

回答

0

如果您从当前的开始210你可以用合并命令添加df[,18]

df2 <- merge(df[,c("first_lat","first_lon","sixth_lat","sixth_lon","col4name","col5name","order_cn")], 
     df2, 
     by.x=c("first_lat","first_lon","sixth_lat","sixth_lon","col4name","col5name"), 
     by.y=c("third_lat","third_lon","fourth_lat","fourth_lon","col4name","col3name"), 
     all.y=TRUE) 

您需要与第四列等的名称,以取代col4name - 我不能从截图什么,可能是看到。该命令的另外五个版本可以轻松生成以添加其他五列。由于该操作在时间上对整个向量有效,因此它可能比循环更快。由于数据没有以合适的格式提供,因此未经测试。