How do I add a vector to another holding the first vector constant?

John-Henry

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.

markus

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I add only the last element of one vector to the last element of another vector?

How do I remove the elements of vector that occur in another vector in Rust?

How do I compare the first element (string) of a pair in a pair vector with another string?

Given a tuple with arbitrary number of vectors holding different types how do I extract the vector with minimum size?

Cannot add constant to vector

How do I copy one vector to another vector in Java without updating the original vector?

How do I export Android XML vector drawables to another format?

Can I add the items of a subvector in a vector to another subvector in the same vector?

How do I add the same integer to each element of a vector in Rust?

How do I add sd field to to the summary of a numeric vector?

In Swift, how do I add a setter to immutable GLKit vector structs?

How do I add an auto_ptr to a vector

How do I add an Android .XML Vector Asset in Xamarin Forms?

How do i add two objects as one element to a vector?

Power Query: How do I add a specific List/Vector as a Column

How do I check whether a vector is equal to another vector that contains NaN and inf?

How do I make a vector in data table that does sums on subsets based value range in another vector?

How do I move a unique pointer from one vector to another vector of unique pointers?

How do I loop over a vector of strings and another vector of numbers at the same time in R?

In R: How do I calculate the averages within a vector over different elements specified by another vector?

How do I apply this for loop over my character vector and output another vector?

How do I find the indices of elements in a vector which are also in another vector using RcppArmadillo?

How to add elements in vector together based on info from another vector

How do I rotate a vector?

How do I maximize a vector

How to add a struct to a vector from another class?

how to erase min element from one vector and add to another, while first becames empty?

How may I sort vectors for one vector by another vector?

How can I order a vector using another vector?