Reputation: 91
I have a data.frame
with 72 monthly time series stacked in long form (vertical). I've used split to create a list
of data.frames
for each series. I now want to take each data.frame
and convert it to an xts
object, using the value
and date
contained within the dataframe. The list has 72 dataframes and I would like a returned list of 72 xts objects. The list looks like:
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
I've tried using llply and lapply to no avail or even ddply. I get errors when trying to write the as.xts function. I think I'm on the right track but my syntax is apparently off.
I've also tried:
lx <- dlply(ur, .(ur$series_id), .fun=(as.xts), ur[,4], order.by=ur[,6])
Pointers?
Upvotes: 2
Views: 1613
Reputation: 176648
I'm not sure what's wrong with your dlply
call, but you should use xts
instead of 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)
Upvotes: 2