If k
has names, those will be used to create the new column names. So you could add names to the vector you pass to k
:
k <- -1:-4
# make.names will ensure the column names are syntactically valid
names(k) <- make.names(paste0("lag",k))
newX <- lag(as.zoo(X), k)
And plot
doesn't know what to do with character strings. You have to pass it an object to plot.
plot(newX$var2.lag.1)