For completeness, a simpler, single-line liner is executed in the "apply" style as requested:
as.list(sapply(x[-1],function(y) as.double(x[1])))
Although this is not the fastest option, it is certainly neat enough to earn its place as an answer to a question. Significant acceleration is possible without unnecessarily simplifying the vector:
library(microbenchmark) microbenchmark(times=20, Charles=as.list(setNames(rep(as.numeric(x[1]), length(x) - 1), x[-1])), Gavin=makeList(x), Anon=sapply(x[-1],function(y) as.double(x[1]),simplify=FALSE) ) Unit: microseconds expr min lq median uq max neval Charles 10.868 11.7735 11.774 12.3775 55.848 20 Gavin 12.075 12.6795 13.132 13.8870 26.867 20 Anon 6.643 7.0950 7.548 8.1520 17.811 20
andrekos
source share