Java Mailing List Archive

http://www.r-help.com/

Home » Home (12/2007) » R Help for Statistical Computing »

[R] do.call("+", ...)

Robin Hankin

2006-11-17

Replies:

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

______________________________________________
R-help@(protected)
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
©2008 r-help.com - Jax Systems, LLC, U.S.A.