Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I lag a data.frame?

I'd like to lag whole dataframe in R.

In python, it's very easy to do this, using shift() function (ex: df.shift(1))

However, I could not find any as an easy and simple method as in pandas shift() in R.

How can I do this?

> x = data.frame(a=c(1,2,3),b=c(4,5,6))
> x
  a b
1 1 4
2 2 5
3 3 6

What I want is,

> lag(x,1)
> 
  a b
1 NA NA
2 1 4
3 2 5

Any good idea?

like image 547
Wookeun Lee Avatar asked Sep 13 '25 03:09

Wookeun Lee


2 Answers

Pretty simple in base R:

rbind(NA, head(x, -1))
   a  b
1 NA NA
2  1  4
3  2  5

head with -1 drops the final row and rbind with NA as the first argument adds a row of NAs.


You can also use row indexing [, like this

x[c(NA, 1:(nrow(x)-1)),]
    a  b
NA NA NA
1   1  4
2   2  5

This leaves an NA in the row name of the first variable, to "fix" this, you can strip the data.frame class and then reassign it:

data.frame(unclass(x[c(NA, 1:(nrow(x)-1)),]))
   a  b
1 NA NA
2  1  4
3  2  5

Here, you can use rep to produce the desired lags

data.frame(unclass(x[c(rep(NA, 2), 1:(nrow(x)-2)),]))
   a  b
1 NA NA
2 NA NA
3  1  4

and even put this into a function

myLag <- function(dat, lag) data.frame(unclass(dat[c(rep(NA, lag), 1:(nrow(dat)-lag)),]))

Give it a try

myLag(x, 2)
   a  b
1 NA NA
2 NA NA
3  1  4
like image 57
lmo Avatar answered Sep 14 '25 18:09

lmo


library(dplyr)
x %>% mutate_all(lag)

   a  b
1 NA NA
2  1  4
3  2  5
like image 36
Lyngbakr Avatar answered Sep 14 '25 16:09

Lyngbakr