I would use a function vapply- it is not much slower than the approach matrix... and I do not like how the outerresult returns. See below:
dd <- 1:5
vapply(seq_along(dd),
FUN = function(i, X) X[[i]] - X,
FUN.VALUE = numeric(length(dd)),
dd)
vf <- function() vapply(seq_along(dd),
FUN = function(i, X) X[[i]] - X,
FUN.VALUE = numeric(length(dd)),
dd)
mf <- function() matrix(dd, length(dd), length(dd), byrow=T) -
matrix(dd, length(dd), length(dd), byrow=FALSE)
microbenchmark(vf(), mf(), times = 1e4)
Unit: microseconds
expr min lq mean median uq max neval cld
vf() 20.527 22.037 26.678118 22.942 24.149 785.434 1000 b
mf() 4.227 4.831 6.343785 5.132 5.434 503.499 1000 a
source
share