x[idxs + 1] or x[idxs + 1L]? That is the question.
Assume that we have a vector x of n=100,000 random values, e.g.
> n <- 100000 > x <- rnorm(n) and that we wish to calculate the n−1 first-order differences y=(y1,y2,…,yn−1) where yi=xi+1−xi. In R, we can calculate this using the following vectorized form:
> idxs <- seq_len(n - 1) > y <- x[idxs + 1] - x[idxs] We can certainly do better if we turn to native code, but is there a more efficient way to implement this using plain R code?