R:与变量中的元素同名的引用对象

发布于 2024-11-03 12:25:20 字数 772 浏览 1 评论 0原文

可能的重复:
R:获取字符串形式的函数名称

1) I有一个变量将数据存储在文本文件的第一列(股票代码)

tickers <- read.csv("stocks.txt", header=FALSE, sep=",")
tickers <- tickers[1]

2)对于我运行的每个代码:getSymbols(tickers, from=startdate, to=enddate)

getSymbols 来自 quantmod 包

调用“getSymbols”的结果是一系列 xts 对象,它们的名称与tickers 变量中的名称相同。

现在我想做的是确定每个 xts 对象中第一个元素的日期。由于每个对象都与tickers变量中与其关联的股票符号具有相同的名称,我想我可以在for循环中执行以下操作,其中i是索引迭代:

min(index(tickers[i]))

但这不起作用,因为tickers[1]返回一个字符名称而不是 index() 所期望的对象。问题是tickers[1]返回的字符是getSymbols创建的xts对象的名称。

我很感激你的帮助。谢谢

Possible Duplicate:
R: getting a function name as a string

1) I have a variable that stores data in the first column of a text file (stock ticker symbols)

tickers <- read.csv("stocks.txt", header=FALSE, sep=",")
tickers <- tickers[1]

2) For each ticker I run: getSymbols(tickers, from=startdate, to=enddate)

getSymbols is from the quantmod package

The result of calling 'getSymbols' is a series of xts objects that have the same name as the names in the tickers variable.

Now what I want to do is determine the date of the first element in each of the xts objects. Since each object has the same name as the ticker symbol associated with it in tickers variable I thought I could just do the following in a for loop where i is the index iteration:

min(index(tickers[i]))

However this does not work because tickers[1] returns a character name and not an object which index() is expecting. The problem is the character returned by tickers[1] is the name of the xts object created by getSymbols.

I appreciate the help. Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅渡 2024-11-10 12:25:20

getSymbols 的分配方式相当奇怪。您想要的是时间序列列表,而不是大量单个变量。巧合的是,(提示无耻的自我推销)我写了关于如何上周创建了这个

作为替代方案,您可以使用 get,如 Ramnath 建议的那样。

sapply(tickers, function(x) min(index(get(x))))

getSymbols is rather odd in how it assigns things. What you want is a list of time series, rather than a load of individual variables. Coincidentally, (cue shameless self-promotion) I wrote about how to create this last week.

As an alternative, you can use get, as Ramnath suggested.

sapply(tickers, function(x) min(index(get(x))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文