优化r中的循环条件

时间:2016-02-09 19:33:37

标签: r for-loop

我有2个数据集,其中一个包含30分钟间隔的温度测量值

ordered.temp<-structure(list(time = structure(c(1385244720, 1385246520, 1385248320, 
1385250120, 1385251920, 1385253720, 1385255520, 1385257320, 1385259120, 
1385260920), class = c("POSIXct", "POSIXt"), tzone = ""), temp = c(30.419, 
29.34, 28.965, 28.866, 28.891, 28.866, 28.692, 28.419, 28.122, 
27.85), hoboID = c(2392890L, 2392890L, 2392890L, 2392890L, 2392890L, 
2392890L, 2392890L, 2392890L, 2392890L, 2392890L)), .Names = c("time", 
"temp", "hoboID"), row.names = c(NA, 10L), class = "data.frame")

,我创建的另一个能够将每个测量分配到3小时的箱子中

df<-structure(list(start = structure(c(1385182800, 1385193600, 1385204400, 
1385215200, 1385226000, 1385236800, 1385247600, 1385258400, 1385269200, 
1385280000), class = c("POSIXct", "POSIXt"), tzone = ""), end = structure(c(1385193600, 
1385204400, 1385215200, 1385226000, 1385236800, 1385247600, 1385258400, 
1385269200, 1385280000, 1385290800), class = c("POSIXct", "POSIXt"
), tzone = ""), b = 1:10), .Names = c("start", "end", "b"), row.names = c(NA, 
10L), class = "data.frame")

为简单起见,我创建了一个数据子集,但实际上临时数据帧长460k行,并且每年都在增长。我写了一个for循环来比较temp中的每一行和bin中的行,并从bin数据帧中为其分配相应的b值。

m <- length(ordered.temp$time)
b <- numeric(m)
n <- length(df$start)

for (i in 1:m){
  for (j in 1:n){
    if (df$start[j] < ordered.temp$time[i] & ordered.temp$time[i] <= df$end[j]){
      b[i] <- df$b[j]
      print(i/dim(ordered.temp)[1]*100)
    }
  }
}

使用460k行运行此循环需要很长时间(我运行循环1分钟并计算完成它需要±277小时。因此,必须加快此循环或找到替代方法,如果这是不可能的。但我不知道如何达到预期的效果。非常感谢任何帮助。谢谢。

0 个答案:

没有答案