Home > other >  Is there a vectorised version of `Filter` (or `purrr:keep`) in R?
Is there a vectorised version of `Filter` (or `purrr:keep`) in R?

Time:11-18

Filter seems slower than the vectorised version in the following example:

u = rnorm(1000000)

system.time(u[u > 0])
# utilisateur     système      écoulé 
#       0.02        0.00        0.01 

system.time(Filter(\(x) x > 0, u))
# utilisateur     système      écoulé 
#       0.71        0.00        0.72 

Is there a faster function than Filter in this case (purrr::keep is even slower)?

CodePudding user response:

It is subjective, but subset seems to convey a better message and it is not that slow as compared to Filter in this case

> system.time(subset(u, u > 0))
   user  system elapsed 
  0.022   0.005   0.027
> system.time(Filter(\(x) x > 0, u))
   user  system elapsed 
  0.579   0.008   0.587  

Or may also do

library(magrittr)
> system.time(u %>% `[`(. > 0))
   user  system elapsed 
  0.012   0.003   0.016 

CodePudding user response:

Note that if you really do want to collect the > 0 realizations from a distribution, you could also take the absolute value of half of the original number of realizations:

system.time(rnorm(1000000))
   user  system elapsed 
   0.06    0.00    0.07 
system.time(abs(rnorm(500000)))
   user  system elapsed 
   0.03    0.00    0.03 
  • Related