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, -james
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))
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
Apparently Analagous Threads
- simulation of modified bartlett's test
- cumulative sum in data frame
- nls for sum of exponentials
- Convertir programa Matlab a R sacado de Threshold Models of Collective Behavior de Michèle Lai & Yann Poltera
- Solve system of equations (nleqslv) only returns origin