[ACCEPTED]-Lagging Variables in R-time-series

Accepted answer
Score: 21

You can achieve this using the built-in 8 embed() function, where its second 'dimension' argument 7 is equivalent to what you've called 'lag':

x <- c(NA,NA,1,2,3,4)
embed(x,3)

## returns
     [,1] [,2] [,3]
[1,]    1   NA   NA
[2,]    2    1   NA
[3,]    3    2    1
[4,]    4    3    2

embed() was 6 discussed in a previous answer by Joshua Reich. (Note 5 that I prepended x with NAs to replicate 4 your desired output).

It's not particularly 3 well-named but it is quite useful and powerful 2 for operations involving sliding windows, such 1 as rolling sums and moving averages.

Score: 9

Use a proper class for your objects; base R has 11 ts which has a lag() function to operate on. Note 10 that these ts objects came from a time when 9 'delta' or 'frequency' where constant: monthly 8 or quarterly data as in macroeconomic series.

For 7 irregular data such as (business-)daily, use 6 the zoo or xts packages which can also deal (very 5 well!) with lags. To go further from there, you 4 can use packages like dynlm or dlm allow for dynamic 3 regression models with lags.

The Task Views 2 on Time Series, Econometrics, Finance all 1 have further pointers.

Score: 2

The running function in the gtools package does more 1 or less what you want:

> require("gtools")
> running(1:4, fun=I, width=3, allow.fewer=TRUE)

$`1:1`
[1] 1

$`1:2` 
[1] 1 2

$`1:3` 
[1] 1 2 3

$`2:4` 
[1] 2 3 4
Score: 1

The method that works best for me is to 1 use the lag function from the dplyr package.

Example:

> require(dplyr)
> lag(1:10, 1)
 [1] NA  1  2  3  4  5  6  7  8  9
> lag(1:10, 2)
 [1] NA NA  1  2  3  4  5  6  7  8

More Related questions