[R] sum a list of vectors

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Dec 13 00:12:03 CET 2002


"Stoyanov, Tsvetan" <tsvetan.stoyanov at mirant.com> writes:

> In Mathematica there is a neat feature, where you can change the
> head of a list from "list" to say "+" and obtain a sum of the list
> elements. I can't find a way to sum a list of vectors of same length
> or list of matrices of the same dimension and was curious if
> something like that exists in R. do.call("+",list) doesn't work
> because "+" accepts only two arguments, I can obviously use loops
> but this is quite slow and inelegant.

 psum <- function(...)apply(cbind(...),1,sum)
 psum(1:3,4:6,1:9)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list