A faster version of an application type (i.e. a multivariate version)
One of the major performance bottlenecks jsonlite
turns out to be applicable, so I was wondering if there are more efficient alternatives for applying functions on rows or columns of a matrix.
For one-dimensional structs, it is vapply
often significantly faster than lapply
or sapply
, since it can allocate output space at the beginning, rather than trying to simplify the output lists at the very end:
> x <- runif(5e6)
> system.time(y1 <- sapply(x, sqrt))
user system elapsed
7.231 0.142 7.370
> system.time(y2 <- vapply(x, sqrt, numeric(1)))
user system elapsed
1.314 0.095 1.410
However vapply
does not work for matrix structures. Is there a similar feature for the family apply
? Currently, the type of output apply
depends on FUN
, for example:
#returns vector
apply(volcano, 1, mean)
#returns matrix
apply(volcano, 1, fivenum)
#returns list
apply(volcano, 1, function(x){list(mean=mean(x), sd=sd(x))})
This script has a simple example apply
in action.
source to share
No one has answered this question yet
Check out similar questions: