我有一个 data.frame
,其中包含以长形式(垂直)堆叠的 72 个每月时间序列。我使用 split 为每个系列创建 data.frames
的 list
。我现在想要使用 value
和 date
获取每个 data.frame
并将其转换为 xts
对象包含在数据框中。该列表有 72 个数据帧,我想要一个包含 72 个 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])
指针?
最佳答案
我不确定您的 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)
关于r - data.frames 列表到 xts 对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18024599/