2013-08-02 61 views
2

我有一個data.frame 72月時間序列堆疊長形式(垂直)。我已經使用split爲每個系列創建listdata.frames。我現在想要採用每個data.frame並使用數據幀中包含的valuedate將其轉換爲xts對象。該列表有72個數據框,我想要一個72個xts對象的返回列表。該列表看起來像:列表data.frames的列出XTS的對象

LAUPS55030003:'data.frame': 282 obs. of 6 variables: 
..$ series_id  : chr [1:282] "LAUPS55030003" "LAUPS55030003" "LAUPS55030003"  
... 
..$ year   : int [1:282] 1990 1990 1990 1990 1990 1990 1990 1990 1990 1990 ... 
..$ period  : chr [1:282] "01" "02" "03" "04" ... 
    ..$ value   : num [1:282] 12.6 5.3 5.3 4.7 4.7 4.7 5.5 4.6 4.6 4.3 ... 
    ..$ footnote_codes: chr [1:282] "" "" "" "" ... 
    ..$ date   : Date[1:282], format: "1990-01-01" "1990-02-01" "1990-03-01" "1990-0 

我試過使用llply和lapply無濟於事,甚至ddply。嘗試編寫as.xts函數時出現錯誤。我認爲我在正確的軌道上,但我的語法顯然是關閉的。

我也試過:

lx <- dlply(ur, .(ur$series_id), .fun=(as.xts), ur[,4], order.by=ur[,6]) 

指針?

回答

2

我不確定您的dlply電話有什麼問題,但您應該使用xts而不是as.xts

ur <- split(LAUPS55030003, LAUPS55030003$series_id) 
xl <- lapply(ur, function(x) xts(x$value, x$date)) 
# and if you want each series in a column: 
xc <- do.call(merge, xl)