[R] Merging lot of zoo objects

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Tue Jul 21 10:42:03 CEST 2009


On Tue, 21 Jul 2009, Michael Knudsen wrote:

> On Tue, Jul 21, 2009 at 9:07 AM, RON70<ron_michael70 at yahoo.com> wrote:
>
>> I have 100 price data series like price1, price2, price3, ............. All
>> are "zoo" objects. Now I want to merge all them together. Obviously I can do
>> this using "merge(price1, price2, price3, ........)". However as I have lot
>> of price series (almost 1000) above systax is very tiresome. Is there any
>> other way on doing to in one-go?
>
> How did you get the names price1, price2, ..., price_100 in the first
> place? Did you make 100 lines of code? If you had stored the objects
> in a list, such that
>
> priceN = list_of_prices[[N]]
>
> you could easily define a recursive function to do the job for you.

You don't need to define it, you can then do

   all_prices <- do.call("merge", list_of_prices)
   colnames(all_prices) <- ...

The resulting "zoo" object will have ugly column names due to the way 
merge() got called but I guess it would be easy for you to come up with 
better names.

> Would it difficult for you to read the data into a list?

I agree that it is preferable to read the data into a list in the first 
place. If that is not possible, something like this might work:

   list_of_prices <- lapply(1:N,
     function(i) get(paste("price", i, sep = "")))

hth,
Z

> When dealing with only a few sets, numbering objects as you do is no
> problem, but for many objects it can become very cumbersome.
>
> -- 
> Michael Knudsen
> micknudsen at gmail.com
> http://lifeofknudsen.blogspot.com/
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>




More information about the R-help mailing list