从环境中获取 xts 对象 [英] get xts objects from within an environment
问题描述
我在环境中存储了 xts 对象.我可以在这些对象存储在环境中时对其进行子集化,即就地"对它们进行操作吗?我可以通过引用它们的 colname 来提取这些对象吗?
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"
我有两个不同的目标.
- 对环境中的所有数据进行子集化(或者就地行动或创造新环境并覆盖旧环境用它).
- 仅选取我选择的一些股票代码并将其子集化,说 FEDFUNDS 和 DGS10,然后将它们保存在一个新的环境.我还想保留这些对象的 xts-ness,以便我可以方便地将它们一起或单独绘制.
以下是我设法做到的一些事情:
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="/")]
这种方法的缺点是我需要在 data$ 之后显式输入 FEDFUNDS.但我想从预先指定的代码列表中工作,例如
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")
通过将函数 get 与函数 lapply
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)
但这会返回一个列表.而且我不确定如何方便地使用此列表来对数据进行子集化、绘制等.我如何引用 DGS10 或tickers2 中的一对代码?
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?
我非常想写一些类似 data$tickers[1] 或 data$tickers[[1]] 的东西,但是没有成功.我还尝试了 paste0('data','$',tickers[1]) 以及带或不带引号的变体.无论如何,我相信环境中数据的顺序不是系统的,所以我真的更喜欢使用股票代码的名称而不是它的索引,比如 data$tickers[colnames = FEDFUNDS] 本段中的所有尝试都没有奏效.
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!
子集
我收到了一些很棒的建议.GSee 的回答有几个非常有用的技巧.以下是将 xts 对象子集化到感兴趣的日期间隔内的方法:
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))
推荐答案
这将对环境中的每个对象进行子集化,并返回具有子集化数据的环境:
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="/")))
对于第二个问题,您基本上可以做同样的事情.只需将 lapply
返回的列表的组件命名为 setNames
,然后强制转换到环境:
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))
或者,更好的是,使用 mget
这样您就不必使用 lapply
或 setNames
Or, better yet, use mget
so that you don't have to use lapply
or setNames
data3 <- as.environment(mget(tickers, envir = data))
<小时>
或者我实际上在 qmao代码>
专门为此设计:gaa
代表获取、申请、分配"和 gsa
代表获取,子集,分配".
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
允许您在保存在相同或不同环境中之前将任何函数应用于每个对象.
gaa
lets you apply any function to each object before saving in the same or different environment.
这篇关于从环境中获取 xts 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!