[英]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.