[R] do.call("+", ...)
Robin Hankin
r.hankin at noc.soton.ac.uk
Fri Nov 17 10:24:42 CET 2006
Hi
How do I make do.call() take "+" as a function for a list of more
than two elements?
Toy problem follows:
f <- function(i){matrix((1:6)^i,2,3)}
# Thus f() returns a matrix of size 2x3; I want to add a whole bunch
of such matrices,
# as in f(1) + f(2) + f(3) + f(4)
# But:
> do.call("+",sapply(1:4,f,simplify=FALSE))
Error in do.call("+", sapply(1:4, f, simplify = FALSE)) :
operator needs one or two arguments
Also,
> do.call(sum,sapply(1:4,f,simplify=FALSE))
[1] 2828
doesn't do what I want (I would like a 2x3 matrix whose elements are the
sum of corresponding elements in my list)
How to do this nicely?
--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel 023-8059-7743
More information about the R-help
mailing list