[R] Sum vectors and numbers

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jul 7 17:51:51 CEST 2010


On Wed, Jul 7, 2010 at 11:35 AM,  <guox at ucalgary.ca> wrote:
> We want to sum many vectors and numbers together as a vector if there is
> at least one vector in the arguments.
> For example, 1 + c(2,3) = c(3,4).
> Since we are not sure arguments to sum, we are using sum function:
> sum(v1,v2,...,n1,n2,..).
> The problem is that sum returns the sum of all the values present in its
> arguments:
> sum(1,c(2,3))=6
> sum(1,2,3)=6
> We do not want to turn sum(v1,v2,...,n1,n2,..) to v1+v2+...+n1+n2+...
> So do you know easy way to sum vectors (v1,v2,...) and numbers (n1,n2,...)
> as a vector without using v1+v2+...+n1+n2+...? Thanks,

Try this:


> L <- list(1:3, 3, 4:6, 6)
> Reduce("+", L)
[1] 14 16 18



More information about the R-help mailing list