从环境中获取xts对象 [英] get xts objects from within an environment

查看:151
本文介绍了从环境中获取xts对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在环境中存储了xts对象。我可以在存储在环境中时对这些对象进行分类,即对其进行就地处理吗?我可以通过引用它们的 colname 来提取这些对象吗?



下面是我所得到的一个例子。

 #存储数据的环境
数据< - new.env()

#设置数据计数器
tickers< -c(FEDFUNDS,GDPPOT,DGS10)

#从FRED数据库导入数据
library(quantmod)
dta< - getSymbols(tickers
,src =FRED
,env = data
,adjust = TRUE

然而,这会下载整个数据集。现在,我想丢弃一些数据,保存它,使用它(例如绘制它)。我想保留这个日期范围内的数据:

 #设置感兴趣的日期
date.start< - 2012-01-01
date.end< - 2012-12-31



我有两个不同的目标。


  1. 将环境内的所有数据(无论是
    就地操作还是创建新环境并覆盖
    旧环境)。

  2. 只能选择一些我选择的代码并将它们分组,
    表示FEDFUNDS和DGS10,然后将它们保存在新的
    环境中。我还想保留这些对象的xts-ness,所以我可以方便地将它们绘制在一起或分开。

下面是一些东西我确实设法做到:
$ b $ pre $ #提取和子集单个xts对象
dtx1< - data $ FEDFUNDS
dtx1 < - dtx1 [paste(date.start,date.end,sep =/)]

这种方法的缺点是我需要在数据$后面显式键入FEDFUNDS。但我想从预先确定的代理商列表中进行工作,例如

  tickers2 < -  c(FEDFUNDS,DGS10)

通过将函数 get 与函数 lapply 结合起来, / p>

 #提取xts对象作为列表
dtxl< - lapply(tickers,get,envir = data)

但是这会返回一个列表。我不确定如何方便地使用这个列表来处理数据的子集,绘制它,等等。我如何引用DGS10或tickers2中的一对代码?

我非常想写一些像数据$ tickers [1] 数据$ tickers [[1]] 的东西,但这不起作用。我还尝试了 paste0('data','$',tickers [1])以及它带有或不带引号的变体。无论如何,我相信环境中的数据顺序并不是系统的,所以我更愿意使用自动收报器的名称而不是索引,就像 data $ tickers [colnames = FEDFUNDS] 本段中没有任何尝试能奏效。



如果我的问题不清楚,我很抱歉,但请确认要求。感谢您的关注!



编辑:子设置



一些奇妙的建议。 GSee 的答案有几个非常有用的技巧。以下是如何将xts对象分到感兴趣的日期间隔内:

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




<$ p

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

对于第二个问题,您可以做基本相同的事情。只需将 lapply 列表中的组件通过用 setNames 包装来返回,然后强制到一个环境:

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

或者,最好使用 mget '必须使用 lapply setNames



<$ p $ (mget(tickers,envir = data))


$ b $ data3< - as.environment b




或者,我实际上在 qmao 专为此设计: gaa 代表get ,apply,assignand gsa 代表get,subset,assign。

,获取一些代码的数据,子集数据,然后分配到一个环境中。

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

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


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

解决方案

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.

这篇关于从环境中获取xts对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆