I’m generating a large amount of lagged variables (365, actually) for a time series with 79,000 observations.
I currently have a column in a dataframe OrdersData called prospectdrops. I first convert it to zoo format to use the lag() function:
prospectdrops<-zoo(OrdersData$prospectdrop)
then I execute the for loop:
for (i in 1:365){
prospectdrops[paste("lag",i,"day",sep="")] <- lag(prospectdrops,i*24,na.pad=TRUE)
}
then I’m forced to loop again using cbind to bind them to prepare them for data.frame()
for (i in 1:365){
cbind(prospectdrops, prospectdrops[paste("lag",i,"day",sep="")])
}
Understandably, this takes forever with the for loop function in R. I know that “apply” could be the answer, but don’t see a direct comparator in the descriptions of the functions. Any ideas?
kinlag.zoocan be a vector. See?lag.zoo.