Reputation: 159
I have a simple integer vector
a<- c(5, 11, 20)
I want to apply multiple operations on it, e.g.,
a1<- a+1
a2<- a+2
a3<- a-3
... and then combine the new vectors element wise. In this example, it would be:
new_a<-c(rbind(a1,a2,a3))
Since my original vector is quite large (~10,000), and the operations I want to apply are quite a few (~20), I am wondering if there is a more compact way to do the same?
Upvotes: 0
Views: 157
Reputation:
You can use outer
function:
a <- c(5, 11, 20)
op <- c(1, 2, -3)
new_a
# [1] 6 7 2 12 13 8 21 22 17
as.vector(t(outer(a, op, "+")))
# [1] 6 7 2 12 13 8 21 22 17
Upvotes: 4