get xts objects from within an environment

2019-02-18 03:38发布

问题:

I have stored xts objects inside an environment. Can I subset these objects while they are stored in an environment, i.e. act upon them "in-place"? Can I extract these objects by referring to their colname?

Below an example of what I'm getting at.

# environment in which to store data 
data <- new.env()

# Set data tickers of interest
tickers <- c("FEDFUNDS", "GDPPOT", "DGS10")

# import data from FRED database
library("quantmod")
dta <- getSymbols( tickers
  , src = "FRED"
  , env = data
  , adjust = TRUE
)

This, however, downloads the entire dataset. Now, I want to discard some data, save it, use it (e.g. plot it). I want to keep the data within this date range:

# set dates of interest
date.start <- "2012-01-01"
date.end <- "2012-12-31"

I have two distinct objectives.

  1. to subset all of the data inside of the environment (either acting in-place or creating a new environment and overwriting the old environment with it).
  2. to take only some tickers of my choosing and to subset those, say FEDFUNDS and DGS10, and afterwards save them in a new environment. I also want to preserve the xts-ness of these objects, so I can conveniently plot them together or separately.

Here are some things I did manage to do:

# extract and subset a single xts object 
dtx1 <- data$FEDFUNDS
dtx1 <- dtx1[paste(date.start,date.end,sep="/")]

The drawback of this approach is that I need to type FEDFUNDS explicitly after data$. But I'd like to work from a prespecified list of tickers, e.g.

tickers2 <- c("FEDFUNDS", "DGS10")

I have got one step closer to being systematic by combining the function get with the function lapply

# extract xts objects as a list
dtxl <- lapply(tickers, get, envir = data)

But this returns a list. And I'm not sure how to conveniently work with this list to subset the data, plot it, etc. How do I refer to, say, DGS10 or the pair of tickers in tickers2?

I very much wanted to write something like data$tickers[1] or data$tickers[[1]] but that didn't work. I also tried paste0('data','$',tickers[1]) and variations of it with or without quotes. At any rate, I believe that the order of the data inside an environment is not systematic, so I'd really prefer to use the ticker's name rather than its index, something like data$tickers[colnames = FEDFUNDS] None of the attempts in this paragraph have worked.

If my question is unclear, I apologize, but please do request clarification. And thanks for your attention!

EDIT: Subsetting

I've received some fantastic suggestions. GSee's answer has several very useful tricks. Here's how to subset the xts objects to within a date interval of interest:

dates <- paste(date.start, date.end, sep="/")
as.environment(eapply(data, "[", dates))

回答1:

This will subset every object in an environment, and return an environment with the subsetted data:

data2 <- as.environment(eapply(data, "[", paste(date.start, date.end, sep="/")))

You can do basically the same thing for your second question. Just, name the components of the list that lapply returns by wrapping it with setNames, then coerce to an environment:

data3 <- as.environment(setNames(lapply(tickers, get, envir = data), tickers))

Or, better yet, use mget so that you don't have to use lapply or setNames

data3 <- as.environment(mget(tickers, envir = data))

Alternatively I actually have a couple convenience functions in qmao designed specifically for this: gaa stands for "get, apply, assign" and gsa stands for "get, subset, assign".

To, get data for some tickers, subset the data, and then assign into an environment

gsa(tickers, subset=paste(date.start, date.end, sep="/"), env=data, 
    store.to=globalenv())

gaa lets you apply any function to each object before saving in the same or different environment.



回答2:

If I'm reading the question correctly, you want smth like this:

dtxl = do.call(cbind, sapply(tickers2,
           function(ticker) get(ticker, env=data)[paste(date.start,date.end,sep="/")])
       )