循环功能运行非常缓慢

时间:2018-06-01 15:26:25

标签: r loops dataframe for-loop dplyr

任何人都可以帮助使这个循环功能更快地运行。目前计算时间太长。

Acceleration <- c(0.16, 0.37, 0.37, 0.48,   1.05,   1.05,   1.93,   2.04,   2.04,   2.07,   2.35,   2.35,   2.03,   1.93,   1.93,   1.75,   1.82,   1.82,   1.49,   0.82,   0.82,   0.34,   -1.69,  -1.69,  -2.62,  -2.38,  -2.38,  -2.01,  -0.86,  -0.86,  1.14,   0.98,   0.98,   1.69,   1.64,   1.64,   2.16,   2.43,   2.43,   2.52,   2.89,   2.89,   2.25,   2.28,   2.28,   1.76,   1.09,   1.09,   1.56,   1.44,   1.44,   0.85,   1.35,   1.35,   0.78,   0.38,   0.38,   0.11,   0.14,   0.14,   -0.78)

Velocity <- c(1.67, 1.77,   1.77,   1.91,   2.19,   2.19,   2.82,   3.05,   3.05,   3.47,   3.79,   3.79,   4.1,    4.26,   4.26,   4.55,   4.76,   4.76,   4.81,   4.8,    4.8,    4.69,   3.86,   3.86,   3.32,   2.89,   2.89,   2.8,    2.91,   2.91,   3.62,   3.67,   3.67,   4.2,    4.34,   4.34,   4.95,   5.27,   5.27,   5.8,    6.2,    6.2,    6.46,   6.69,   6.69,   6.86,   6.76,   6.76,   7.15,   7.26,   7.26,   7.3,    7.59,   7.59,   7.67,   7.59,   7.59,   7.45,   7.48,   7.48,   7.16)

Test <- data.frame(Acceleration,Velocity) 

这是带循环的计算列。

 Test$Accels[1] <- 0
for(i in 2:nrow(Test)) 
 {Test$Accels[i] <- 
     if(Test$Acceleration[i] <= 0) { 0 } 
     else if(Test$Acceleration[i] >= 2 & Test$Acceleration[i+1] >= 2 & Test$Acceleration[i+2] >= 2 & Test$Acceleration[i+3] >= 2 & Test$Acceleration[i+4] >= 2 &  
         Test$Accels[i-1] == 0) { 2 } 
     else if(Test$Accels[i-1] > 0) { 1 }
     else 0}

期望的输出:

Test$Accels <- c(0, 0,  0,  0,  0,  0,  0,  2,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  2,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  0)

任何人都可以帮我重新编写Test $ Accels列代码,以使其运行得更快吗?

在数据框中的另一个计算列中,我有时会使用以下代码:

 if(Test$Velocity[i] < 1.4 | Test$Velocity[i+1] < 1.4 | Test$Velocity[i+2] < 1.4 | Test$Velocity[i+3] < 1.4 | Test$Velocity[i+4] < 1.4 ) {0}

任何人都可以帮我重新编写这部分代码吗?更快/更短?

1 个答案:

答案 0 :(得分:0)

使用sapply为我加速。它不短但更快。

microbenchmark::microbenchmark(
Test$Accels[2:nrow(Test)] <- sapply(2:nrow(Test), function(i){

  if(Test$Acceleration[i] <= 0) { 0 } 
else if(Test$Acceleration[i] >= 2 & Test$Acceleration[i+1] >= 2 & Test$Acceleration[i+2] >= 2 & Test$Acceleration[i+3] >= 2 & Test$Acceleration[i+4] >= 2 &  
        Test$Accels[i-1] == 0) { 2 } 
else if(Test$Accels[i-1] > 0) { 1 }
else 0})
)
microbenchmark::microbenchmark(
for(i in 2:nrow(Test)) 
{Test$Accels[i] <- 
  if(Test$Acceleration[i] <= 0) { 0 } 
else if(Test$Acceleration[i] >= 2 & Test$Acceleration[i+1] >= 2 & Test$Acceleration[i+2] >= 2 & Test$Acceleration[i+3] >= 2 & Test$Acceleration[i+4] >= 2 &  
        Test$Accels[i-1] == 0) { 2 } 
else if(Test$Accels[i-1] > 0) { 1 }
else 0})