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 n1 first-order differences y=(y1,y2,,yn1) where yi=xi+1xi. 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?

Continue reading

Author's picture

Henrik Bengtsson

MSc CS | PhD Math Stat | Associate Professor | R Foundation | R Consortium

Associate Professor