How to sum values of two unequal vectors in R?

Viewed 108

I have two unequal vectors in length. For example, I want to add all values from TT to all values from FF.

TT <- c(1:10)
FF <- c(0, 60, 120, 180)

I would expect to have the below result

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190

I would appreciate if you could give me some advice.

Thanks in advance

1 Answers

We can use outer

c(outer(TT, FF, FUN = `+`))

or with sapply

c(sapply(TT, `+`, FF))
Related