linmu
linmu

Reputation: 11

Merging a lot of xts objects

I have some 'xts' objects, nearly 600, and I want to merge them to one 'xts'object.

The names of these objects are something like mfi0, mfi1, mfi2,... . I can write merge command for a few objects such as

a<-merge(mfi0,mfi1,mfi2) 

but my problem is that I couldn't write a loop or lapply for 600 objects, containing string "mfi" and a number, I tried paste but couldn't write.

Upvotes: 0

Views: 629

Answers (2)

Leonardo Motta
Leonardo Motta

Reputation: 121

Place all xts objects in a list list.xts. In your case, it will be a list of 600 elements each of which is a xts object. Then you can simply use

a = Reduce(f=merge.xts, list.xts)

Upvotes: 0

Pieter
Pieter

Reputation: 3447

Where do these objects come from? It is better to use a list in the generation process instead of 600 seperate variables. Thus instead of having mfi0, mfi1, ..., mfi600 you should have mfi[1], mfi[2], ..., mfi[600].

Having all your data helps a lot in handling it. The merge procedure is now much simpler using your own merge command:

merged_mfi <- do.call(merge, mfi)

Or using a merge command that only handles two objects (i.e. the R merge for data.frames):

merged_mfi <- Reduce(merge, mfi[2:length(mfi)], mfi[1])

Upvotes: 2

Related Questions