使用两个if语句加速循环

时间:2015-05-13 15:50:09

标签: r for-loop data.table

我有一个包含15,000多行的数据表DT。我有一个正确运行的for循环,但它需要30秒以上,并且是整个代码中最慢的部分。这是for循环:

for (i in 2:nrow(DT)) {
 if(DT$C1[i] == DT$C1[i+1] & DT$C2[i] != DT$C2[i+1] & DT$C3[i+1] - DT$C3[i] <= 4 & DT$C2[i] == "Short" & DT$C2[i+1] != "Long") DT$C4[i] = 1 else 
  if(DT$C1[i] == DT$C1[i-1] & DT$C2[i] != DT$C2[i-1] & DT$C3[i] - DT$C3[i-1] <= 4 & DT$C2[i] == "Short" & DT$C2[i-1] != "Long") DT$C4[i] = 1 else
      0 }

有没有办法加快速度?我已经看到了一些示例here和其他地方,但它们并没有完全解决我i+1i-1的特定问题。

以下是一些示例数据。

C1 <- c("1","1","1","1","1","2","2","2","3","3","3","3","3","3","3","3","4","4","4","4","4","4","4","4","4","4","4","4","4")
C2 <- c("Short","Short","Short","None","None","Short","Short","Other","Long","Long","Long","Long","Long","Long","Long","Long","Short","Short","Other","Short","Short","None","Short","None","Other","Short","Short","Short","Short")
C3 <- c(as.Date("2010-06-01"),as.Date("2010-06-05"),as.Date("2010-06-09"),as.Date("2010-06-13"),as.Date("2010-06-17"),as.Date("2010-06-02"),as.Date("2010-06-21"),as.Date("2010-07-09"),as.Date("2010-07-13"),as.Date("2010-07-17"),as.Date("2010-07-21"),as.Date("2010-08-01"),as.Date("2010-08-05"),as.Date("2010-08-09"),as.Date("2010-09-03"),as.Date("2010-09-07"),as.Date("2010-06-03"),as.Date("2010-06-07"),as.Date("2010-06-11"),as.Date("2010-06-14"),as.Date("2010-06-17"),as.Date("2010-06-21"),as.Date("2010-06-24"),as.Date("2010-06-27"),as.Date("2010-07-01"),as.Date("2010-07-05"),as.Date("2010-07-09"),as.Date("2010-07-13"),as.Date("2010-07-17"))

DF <- data.frame(C1=C1, C2=C2, C3=C3)
DT <- as.data.table(DF)

和期望的输出。

C1  C2      C3          C4
1   Short   2010-06-01  0
1   Short   2010-06-05  0
1   Short   2010-06-09  1
1   None    2010-06-13  0
1   None    2010-06-17  0
2   Short   2010-06-02  0
2   Short   2010-06-21  0
2   Other   2010-07-09  0
3   Long    2010-07-13  0
3   Long    2010-07-17  0
3   Long    2010-07-21  0
3   Long    2010-08-01  0
3   Long    2010-08-05  0
3   Long    2010-08-09  0
3   Long    2010-09-03  0
3   Long    2010-09-07  0
4   Short   2010-06-03  0
4   Short   2010-06-07  1
4   Other   2010-06-11  0
4   Short   2010-06-14  1
4   Short   2010-06-17  1
4   None    2010-06-21  0
4   Short   2010-06-24  1
4   None    2010-06-27  0
4   Other   2010-07-01  0
4   Short   2010-07-05  1
4   Short   2010-07-09  0
4   Short   2010-07-13  0
4   Short   2010-07-17  0

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容对其进行矢量化:

n <- nrow(DT)
DT$C4 <- NA  # Initialize however you want
# Warning -- untested due to no reproducible example...
DT$C4[2:(n-1)] <- as.numeric((DT$C1[2:(n-1)] == DT$C1[3:n] & DT$C2[2:(n-1)] != DT$C2[3:n] & DT$C3[3:n] - DT$C3[2:(n-1)] <= 4 & DT$C2[2:(n-1)] == "Short" & DT$C2[3:n] != "Long") |
                             (DT$C1[2:(n-1)] == DT$C1[1:(n-2)] & DT$C2[2:(n-1)] != DT$C2[1:(n-2)] & DT$C3[2:(n-1)] - DT$C3[1:(n-2)] <= 4 & DT$C2[2:(n-1)] == "Short" & DT$C2[1:(n-2)] != "Long"))

基本上,每当您在循环中按i编制索引时,都会将其替换为2:(n-1),每次使用i-1编制索引时,都会将其替换为1:(n-2),并且每次您使用i+1编入索引,并将其替换为3:n