Possible Duplicate:
Basic lag in R vector/dataframe
Trying to lag a variable in R but it isn’t working.
x<-1:10
y=lag(x,1)
xy=cbind(x,y)
View(xy)
x y
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10
I am still not getting the lag. What am I doing wrong? Also is there a quicker way to combine to vectors/matrices without using cbind/rbind? For example
x=0:10
y=20:30
newxy=[x,y]
Thank you!
lag()expects a time series. (In R, class “ts” is the basic time-series class, used to represent data sampled at equispaced points in time. For more see?ts.) So you can either convert x to a time-series, as demonstrated here, or make use one of the approaches suggested in another answer.