user1638221
user1638221

Reputation: 25

Merging two data environments and do.call()

consider the following code

require(quantmod)

list.X <- c('ANFCI',  'NFCI', 'STLFSI', 'WAAA', 'WBAA', 'WCOILWTICO','WFII10', 
            'WGS10YR', 'WGS1YR', 'WGS3YR', 'WGS5YR', 'WSWP10', 'WTB3MS')
data.X <- new.env()
getSymbols(list.X, src = 'FRED', from = '1950-01-01', env = data.X)

Now I put those time series in data.X environment; let I need to put in data.X another time series:

getSymbols('SPY', from = '1950-01-01', env = data.X)

By default, this is downloaded from Yahoo. With the last command, did I put it into data.X as I need?

Then let I want to merge them. I believed that something like

X <- do.call(merge, as.list(c(list.X, 'SPY')), envir = data.X)

could work, but that command dosen't work.

Coudl you suggest me something working?

Thanks,

Upvotes: 0

Views: 789

Answers (1)

seancarmody
seancarmody

Reputation: 6290

Try this:

mmerge <- function(.list, all=FALSE, env=.GlobalEnv) {
  .list <- lapply(.list, get, env)
  Reduce(function(...) merge(..., all=all), .list)
}

head(mmerge(list.X, env=data.X))
head(mmerge(as.list(c(list.X, 'SPY')), env=data.X))

with a nod to this question

Upvotes: 3

Related Questions