Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I merge a large list of xts objects via loop / function in R?

Tags:

r

time-series

xts

I have a loop that extracts ~200 individual timeseries by making calls to an API.

The loop outputs the timeseries as xts objects (library(xts)) into the Global Environment with the suffix ".oc". So I have 200 xts objects of the form "ABC.oc", "ABD.oc" etc. Each object contains 1000 rows of data.

What I would like to do is write a loop (or use an appropriate function) that takes all the "*.oc" objects and merges them by column. IE would end up with:

Date           ABC.oc    ABD.oc -> 200 columns like this
2011-01-01      10         10
2011-01-02      20         20
2011-01-03      30         30
2011-01-04      40         40
2011-01-05      50         50

With a short list of timeseries, would just write:

m <- merge(ABC.oc,ABD.oc,all=FALSE)

But obviously this is not practical with 200 individual objects, so I'd like to write a loop to smash all these objects together like "merge" does.

Easy enough to access the variables for the loop via for i in length(ls(pattern="*.oc")){ but just cannot figure out the rest of the loop. I've tried cbind, but can't seem to get it right.

Any help much appreciated

like image 608
n.e.w Avatar asked Jun 16 '11 23:06

n.e.w


2 Answers

This can be accomplished by getting a character vector of all the objects with names ending in .oc, putting them in a list, then calling merge via do.call.

# Make up some data
set.seed(21)
x.oc <- xts(rnorm(10), Sys.Date()-10:1)
y.oc <- xts(rnorm(10), Sys.Date()-10:1)
z.oc <- xts(rnorm(10), Sys.Date()-10:1)
x <- y <- z <- 1:10

objNames <- ls(pattern="*oc$")    # object names
objList <- lapply(objNames, get)  # list of objects
names(objList) <- objNames        # assign names to list
do.call(merge, objList)           # merge all objects in list

Using this method would be easier if you loaded the xts objects into a list (objList) as you received them from the API. Then you would only need do.call(merge, objList).

like image 105
Joshua Ulrich Avatar answered Oct 31 '22 04:10

Joshua Ulrich


A loop like this should work. Always a good idea to initialise it first though.

library(xts)

m <- xts(matrix(vector(length=length(ls(pattern="*.oc")) * 
  nrow(get(ls(pattern="*.oc")[1]), ncol=nrow(get(ls(pattern="*.oc")[1])), 
  order.by=index(get(ls(pattern="*.oc")[1]))

for (i in 1:length(ls(pattern="*.oc"))) {
  m[, i]  <- get(ls(pattern="*.oc")[i])
}
like image 33
wkmor1 Avatar answered Oct 31 '22 04:10

wkmor1