2012-07-21 93 views
1

我正在使用xts并在循环中加载100到1000个文件。每个文件在50k到300k行之间。我在Windows 7 64位上使用R 2.15.1的最新版本。我在使用R版本2.14.X的Ubuntu Linux上遇到同样的问题。xts :: to.period导致R崩溃的问题

下面的代码将定期崩溃R:

library(xts) 
N <- 1e6 
for(i in 1:1000) { 
    allTimes <- Sys.time()-N:1 
    x <- NULL 
    x <- xts(,allTimes) 
    sampTimes <- allTimes[seq(1,length(allTimes),by=2)] 
    y <- merge(xts(seq_along(sampTimes), sampTimes), allTimes) 
    y <- na.locf(y) 
    y <- to.period(y, 'seconds', 10) 
    index(y) <- index(to.period(x, 'seconds', 10)) 
} 
+0

这听起来像你内存不足。你说你在Ubuntu上试过这个,但是你没有说崩溃是否发生。你的Ubuntu机器上有交换空间吗? – 2012-07-22 00:39:01

+0

小心分享您使用的代码?您可以尝试使用浏览器()进行调试。这样,你“进入”功能,并开始探索。其乐无穷。 – 2012-07-22 07:25:53

+0

一些事情。看来R不喜欢使用打印和阅读文件。使用进度条有助于R的行为。 R Studio仍然有问题。适用于Linux和Windows。 – Dave 2012-07-22 18:34:27

回答

4

这是answered on R-devel。该问题在调用to.period零宽度xts对象将返回一个随机存储器位置的OHLC数据。例如:

library(xts) 
x <- xts(,Sys.time()-10:1) 
y <- to.period(x) 
y 
#       x.Open  x.High   x.Low  x.Close 
# 2012-07-23 15:47:30 4.25426e-314 2.36246e-300 1.428936e-316 1.428936e-316 

由于聚集“无数据”没有意义,我已平息to.period抛出异常时对零宽度/长度的对象(修订690上R-锻造)上运行。

而不是在一个零宽度的对象上运行to.period,只需创建一个临时xts对象充满了1,并运行to.period。这将与CRAN上的xts一起使用。

library(xts) 
N <- 1e6 
for(i in 1:100) { 
    allTimes <- Sys.time()-N:1 
    x <- NULL 
    x <- xts(,allTimes) 
    sampTimes <- allTimes[seq(1,length(allTimes),by=2)] 
    y <- merge(xts(seq_along(sampTimes), sampTimes), allTimes) 
    y <- na.locf(y) 
    y <- to.period(y, 'seconds', 10) 
    tmp <- xts(rep(1,length(allTimes)), allTimes) 
    index(y) <- index(to.period(tmp, 'seconds', 10)) 
}