[R] Sum vectors and numbers

Erik Iverson eriki at ccbr.umn.edu
Wed Jul 7 17:42:12 CEST 2010



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,

  I must admit I've read this a couple times and am confused.  Can you 
give one example that shows exactly what you want, using R objects?

I guess my question is, what is wrong with 1 + c(2, 3)

Is this what you're looking for?

lapply(c(1,2,3), `+`, c(8,9,10))



More information about the R-help mailing list