简体   繁体   中英

In R how do I add every value from 1 vector to every value from a 2nd vector?

I have two vectors in R:

vector1 <- c(1:24)

vector2 <- seq.int(.05,.60,.05)

How do I combine these vectors to a third vector in that every value in 1 is added to every value in vector 2?

Meaning the result should look like:

1.05, 1.10, 1.15, 1.20...
24.05,24.10,24.15,24.20...

Any help would be great.

Thanks, Jonathan

You can use outer() here to get the sum of all the combinations. It returns a matrix so I just use c() to make that a vector. If you want to keep it as a matrix, just drop that part.

c(outer(vector2, vector1, `+`))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM