Daniel - 1 year ago 64

R Question

I have two vectors and I want a new vector which elements are the sum of an element of vector 1 and an element of vector 2.

`v1<-c(1,2,3,4,5,6)`

v2<-c(0,1,1,2,2,1)

for(i in 1:length(v1)){

for(j in 1:length(v2)){

n<-vector()

n<-v1[i]+v2[j]

}

m<-NULL

m[n]<-m

}

When I run the loop, I get

`m=NULL`

`n`

`NA`

Answer Source

Perhaps we need

```
tapply(c(v1, v2), c(v1, v2), FUN = sum)
```

Or just

```
v1 + v2
```

Or could be `outer`

```
outer(v1, v2, FUN = "+")
```