Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic lag in R vector/dataframe

Will most likely expose that I am new to R, but in SPSS, running lags is very easy. Obviously this is user error, but what I am missing?

x <- sample(c(1:9), 10, replace = T) y <- lag(x, 1) ds <- cbind(x, y) ds 

Results in:

      x y  [1,] 4 4  [2,] 6 6  [3,] 3 3  [4,] 4 4  [5,] 3 3  [6,] 5 5  [7,] 8 8  [8,] 9 9  [9,] 3 3 [10,] 7 7 

I figured I would see:

     x y  [1,] 4   [2,] 6 4  [3,] 3 6  [4,] 4 3  [5,] 3 4  [6,] 5 3  [7,] 8 5  [8,] 9 8  [9,] 3 9 [10,] 7 3 

Any guidance will be much appreciated.

like image 566
Btibert3 Avatar asked Aug 24 '10 17:08

Btibert3


People also ask

What is LAG () in R?

lag takes an atomic vector and returns that same vector with an added attribute of three numbers indicating the start, end, and frequency of a lag of length one on the vector you supplied.

How do you lag a variable in R?

Lagged variable is the type of variable that contains the previous value of the variable for which we want to create the lagged variable and the first value is neglected. Data can be segregated based on different groups in R programming language and then these categories can be processed differently.

What is the opposite of lag in R?

The opposite of lag() function is lead()


2 Answers

I had the same problem, but I didn't want to use zoo or xts, so I wrote a simple lag function for data frames:

lagpad <- function(x, k) {   if (k>0) {     return (c(rep(NA, k), x)[1 : length(x)] );   }   else {     return (c(x[(-k+1) : length(x)], rep(NA, -k)));   } } 

This can lag forward or backwards:

x<-1:3; (cbind(x, lagpad(x, 1), lagpad(x,-1)))      x       [1,] 1 NA  2 [2,] 2  1  3 [3,] 3  2 NA 
like image 114
Andrew Avatar answered Sep 29 '22 08:09

Andrew


Another way to deal with this is using the zoo package, which has a lag method that will pad the result with NA:

require(zoo) > set.seed(123) > x <- zoo(sample(c(1:9), 10, replace = T)) > y <- lag(x, -1, na.pad = TRUE) > cbind(x, y)    x  y 1  3 NA 2  8  3 3  4  8 4  8  4 5  9  8 6  1  9 7  5  1 8  9  5 9  5  9 10 5  5 

The result is a multivariate zoo object (which is an enhanced matrix), but easily converted to a data.frame via

> data.frame(cbind(x, y)) 
like image 25
Gavin Simpson Avatar answered Sep 29 '22 06:09

Gavin Simpson