Suppose:
x <- c(2,-5)
y <- c(1,2,3)
How can I get :
z = c(2-1, 2-2, 2-3, -5-1, -5-2, -5-3) = c(1, 0, -1, -6, -7, -8)
Using outer
:
> as.vector(outer(x, y, '-'))
# [1] 1 -6 0 -7 -1 -8
And if you want the other way:
> as.vector(t(outer(x, y, '-')))
# [1] 1 0 -1 -6 -7 -8
You can use rep
as follows:
rep(x, each = length(y)) - y
# [1] 1 0 -1 -6 -7 -8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With