如果一个函数返回 2 个或更多值,并且使用fill = NA
,rollapply
会变得慢得多。有什么办法可以避免吗?
f1= function(v)c(mean(v)+ median(v)) #return vector of length 1
f2= function(v)c(mean(v), median(v)) #return vector of length 2
v = rnorm(1000)
microbenchmark(rollapplyr(v, 20, f1), rollapplyr(v,20, f1, fill=NA) )
# expr min lq mean median uq max neval
# rollapplyr(v, 20, f1) 50.84485 53.68726 57.21892 54.63793 57.78519 75.88305 100
# rollapplyr(v, 20, f1, fill = NA) 52.11355 54.69866 59.73473 56.20600 63.10546 99.96493 100
microbenchmark(rollapplyr(v, 20, f2), rollapplyr(v,20, f2, fill=NA) )
# expr min lq mean median uq max neval
# rollapplyr(v, 20, f2) 51.77687 52.29403 56.80307 53.44605 56.65524 105.6713 100
# rollapplyr(v, 20, f2, fill = NA) 69.93853 71.08953 76.48056 72.21896 80.58282 151.4455 100