2012-07-21 100 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)) 
}