Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lag not working as expected

Tags:

r

lag is not working as I expected

a<-c(0,1,2,3,4,5,6,7,8)
a
## [1] 0 1 2 3 4 5 6 7 8
lag(a,k=1)
## [1] 0 1 2 3 4 5 6 7 8
## attr(,"tsp")
## [1] 0 8 1

I thought I would get:

0 0 1 2 3 4 5 6 7 

or

1 2 3 4 5 6 7 8 0

What am I doing wrong?

like image 447
ManInMoon Avatar asked Jan 16 '15 11:01

ManInMoon


1 Answers

You should use Lag from the Hmisc package:

library(Hmisc)
Lag(c(0,1,2,3,4,5,6,7,8), shift = 1)
# [1] NA  0  1  2  3  4  5  6  7
like image 130
Sven Hohenstein Avatar answered Dec 01 '22 16:12

Sven Hohenstein