Reputation: 1807
How do I add a vector to another while keeping for the first vector constant? For example if I had c(1, 2, 3) + 1
. I would get 2, 3, 4. If I wanted to scale this up to say + 1, and + 2, what could I do to get
2, 3, 4, 3, 4, 5
Intuitively I wanted to c(1, 2, 3) + c(1, 2)
but this does not work.
Upvotes: 0
Views: 39
Reputation: 26343
Turning the comments into an answer we can use outer
as @jogo showed
c(outer(1:3, 1:2, FUN='+'))
# [1] 2 3 4 3 4 5
Another option is rep
f <- function(x, y) {
x + rep(y, each = length(x))
}
f(1:3, 1:2)
# [1] 2 3 4 3 4 5
Upvotes: 1