简体   繁体   English

在R中生成滞后变量时使用循环避免循环(并使用动物园)

[英]avoiding for loops when generating lagged variables in R (and using zoo)

I'm generating a large amount of lagged variables (365, actually) for a time series with 79,000 observations. 我正在为79,000个观测值的时间序列生成大量滞后变量(实际上是365)。

I currently have a column in a dataframe OrdersData called prospectdrop s. 我目前在数据OrdersData有一个名为prospectdrop的列。 I first convert it to zoo format to use the lag() function: 我首先将其转换为zoo格式以使用lag()函数:

prospectdrops<-zoo(OrdersData$prospectdrop)

then I execute the for loop: 然后我执行for循环:

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() 然后我被迫再次循环使用cbind绑定它们为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. 可以理解,这需要永远使用R中的for循环函数。我知道“apply”可能是答案,但在函数描述中没有看到直接的比较器。 Any ideas? 有任何想法吗?

k in lag.zoo can be a vector. lag.zoo k可以是向量。 See ?lag.zoo . ?lag.zoo

x <- zoo(11:21)
lag(x,1:3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM