# Cross-difference between arrays without using for loop

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?

## Answers

Since your a and b are the same length, I've assumed you'd like to first trim the last element off of b. (Try b - a[-1] to see why that's probably desirable.)

b[-length(b)] - a[-1] # [1] 0 1 2 3 4 5 6 7 8