Using the following code:
a <- seq(1, 10, 1)
b <- seq(2, 20, 2)
I would like to subtract a[i - 1] from b[i] for each i, in order to obtain something like
c <- NULL
for(i in 1:length(b)) {
c[i] <- b[i] - a[i - 1]
}
but I would like to do this without using for() loop.
Anyone knows how to do it in just one command line?
Since your
aandbare the same length, I’ve assumed you’d like to first trim the last element off ofb. (Tryb - a[-1]to see why that’s probably desirable.)