4

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))
4

2 回答 2

3

这将子集环境中的每个对象,并返回具有子集数据的环境:

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

您可以对第二个问题做基本相同的事情。lapply只需将返回的列表的组件命名为setNames,然后强制转换为环境:

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

或者,更好的是,使用mget这样您就不必使用lapplysetNames

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

或者,我实际上有几个qmao专门为此设计的便利功能: gaa代表“获取、应用、分配”和gsa代表“获取、子集、分配”。

To,获取一些代码的数据,对数据进行子集化,然后分配到环境中

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

gaa允许您在保存到相同或不同环境之前对每个对象应用任何功能。

于 2013-04-12T22:10:39.460 回答
1

如果我正确地阅读了这个问题,你想要这样的东西:

dtxl = do.call(cbind, sapply(tickers2,
           function(ticker) get(ticker, env=data)[paste(date.start,date.end,sep="/")])
       )
于 2013-04-12T22:09:29.327 回答