字符变量作为函数的参数
我下载财务数据的代码的一部分:
library(quantmod)
tickers <- c("XOM", "DIS")
stock1 <- getSymbols(tickers[1], from="2010-03-01", to="2011-02-28", auto.assign=F)
stock2 <- getSymbols(tickers[2], from="2010-03-01", to="2011-02-28", auto.assign=F)
pair <- merge(Ad(stock1), Ad(stock2), all=F) # 'xts' object, merge adjusted close
pair.DF <- data.frame(pair) # data frame
但我想调用这样的函数:
tickers <- function(x, y) {
stock1 <- getSymbols(x, from="2010-03-01", to="2011-02-28", auto.assign=F)
stock2 <- getSymbols(y, from="2010-03-01", to="2011-02-28", auto.assign=F)
pair <- merge(Ad(stock1), Ad(stock2), all=F)
pair.DF <- data.frame(pair)
}
然后:
tickers("XOM", "DIS")
或者类似的东西。但我得到的是不好的结果。在这种情况下,“x”和“y”不是数字,而是字符。 看起来似乎很容易,但是.. 感谢您的任何建议。
part of my code for downloading financial data:
library(quantmod)
tickers <- c("XOM", "DIS")
stock1 <- getSymbols(tickers[1], from="2010-03-01", to="2011-02-28", auto.assign=F)
stock2 <- getSymbols(tickers[2], from="2010-03-01", to="2011-02-28", auto.assign=F)
pair <- merge(Ad(stock1), Ad(stock2), all=F) # 'xts' object, merge adjusted close
pair.DF <- data.frame(pair) # data frame
But I want to call function like this:
tickers <- function(x, y) {
stock1 <- getSymbols(x, from="2010-03-01", to="2011-02-28", auto.assign=F)
stock2 <- getSymbols(y, from="2010-03-01", to="2011-02-28", auto.assign=F)
pair <- merge(Ad(stock1), Ad(stock2), all=F)
pair.DF <- data.frame(pair)
}
And then:
tickers("XOM", "DIS")
Or something similar. But what I get is bad result. In this case "x" and "y" aren´t numeric, but character.
It seems to be easy, but..
Thanks for any suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要你最后返回pair.DF,我就可以工作。这是你的问题吗?
我不认为字符类是问题所在:
Works for me as long as you return the pair.DF at the end. Is that your problem?
I don't think the character class is the problem: