Why is apply() method slower than a for loop in R?

前端 未结 5 1512
既然无缘
既然无缘 2020-11-27 04:55

As a matter of best practices, I\'m trying to determine if it\'s better to create a function and apply() it across a matrix, or if it\'s better to simply loop a

5条回答
  •  无人及你
    2020-11-27 05:27

    It is far faster in this case to do index-based replacement than either the ifelse(), the *apply() family, or the loop:

    > million  <- million2 <- as.matrix(rnorm(100000))
    > system.time(million3 <- ifelse(million > 0, 1, -1))
       user  system elapsed 
      0.046   0.000   0.044 
    > system.time({million2[(want <- million2 > 0)] <- 1; million2[!want] <- -1}) 
       user  system elapsed 
      0.006   0.000   0.007 
    > all.equal(million2, million3)
    [1] TRUE
    

    It is well worth having all these tools at your finger tips. You can use the one that makes the most sense to you (as you need to understand the code months or years later) and then start to move to more optimised solutions if compute time becomes prohibitive.

提交回复
热议问题