R:与变量中的元素同名的引用对象
可能的重复:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getSymbols 的分配方式相当奇怪。您想要的是时间序列列表,而不是大量单个变量。巧合的是,(提示无耻的自我推销)我写了关于如何上周创建了这个。
作为替代方案,您可以使用
get
,如 Ramnath 建议的那样。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.