I want to use the lag
-function of base R because I'm using it in a 1st year BSc-class I teach and don't want to introduce too many packages. However, I can't really understand how it works.
x <- data.frame(1:10)
x$l1 <- lag(x[,1], k=1)
x$l2 <- lag(x[,1], k=-1)
Produce:
X1.10 l1 l2
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 8 8
9 9 9 9
10 10 10 10
I was expecting output that was one element shorter than x[,1]
, and preferably that R inserted a NA for the first obs where I don't have a lagged value.
I want to calculate auto-correlation using the cor
-function, but that doesn't work with this type of behavoir for lag
-function. Example using other data:
cor(Price[,1],lag(Price[,1], k = 1), use = "complete.obs")
This always return a (auto) correlation of 1.
These are 1st year BSc-students so I don't want to use too much packages or commands.
Follow-up: I'm aware of the following possibility, but don't find it satisfactory for my students.
n <- nrow(x)
c(NA,x[1:n-1,1])
[1] NA 1 2 3 4 5 6 7 8 9