I have 2 vectors
x <- c(2,2,5)
y <- c(1,2)
I want to add each element of the vectors together to get
[1] 3 3 6 4 4 7
How can I do this?
We can use outer
with FUN
as +
c(outer(x, y, `+`))
#[1] 3 3 6 4 4 7
You can try creating each pair of x/y elements with expand.grid
and then computing the row sums:
rowSums(expand.grid(x, y))
# [1] 3 3 6 4 4 7
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With