包含多个参数(R)的函数的前循环 [英] For-Loops for functions containing multiple arguments (R)

查看:100
本文介绍了包含多个参数(R)的函数的前循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:我在R中有一个自定义函数,该函数应该获取财务数据(使用quantmod);现在我正在考虑,我可能想获得几家公司的股价,或者如果我可以拥有一个包含我函数的参数的数据框,并且一个循环遍历数据的所有部分,我会发现它更方便框,然后将结果保存到我的环境(或特定的新数据框或其他内容)中.

i have the following problem: I have got a custom defined function in R, which should get finance data (using quantmod); Now that i am thinking about that i maybe want to get several companies' stock prices or so i would it find more convenient if i can have a dataframe which contains the arguments of my function and a loop just goes through all the parts of the data frame and then saves the results into my environment (or a specific new dataframe or whatever).

我的代码的相关部分:

#Define Custom Function to get Data
pull = function(abbreviation,from,to){
  getSymbols(Symbols = abbreviation, from = as.Date(from), to = as.Date(to),env = .GlobalEnv, reload.Symbols = FALSE, verbose = FALSE, warnings = TRUE, src = "yahoo", symbol.lookup = TRUE, auto.assign = TRUE)
  #return(abbreviation_data) ##This part did not work but that should not be the relevant one inn this function as the function itself works;
}

为了进行测试,我现在定义了要循环的数据:

For testing i defined my data now which shall be looped:

abbreviation = c("MSFT","AAPL")
from = c("2010-01-01","2011-01-01")
to = c("2017-04-19","2017-04-19")
stocks = data.frame(abbreviation,from,to)

现在出现问题的行:

for (i in 1:nrow(stocks)){
  pull(stocks[i,1],stocks[i,2],stocks[i,3])}

您可能已经看到我是R的绝对初学者;希望您能给我一个答案,我如何使它工作以及如何将其输入到诸如数据帧或smth的输出中.这样(就像原始的getSymbols函数一样)

as you maybe already saw i am an absolute beginner in R; Hopefully you can give me an answer how i get this one working and how i can get it into an output like a dataframe or smth. like that (like the original getSymbols-function does)

谢谢您的帮助!

推荐答案

这是一个将applyMARGIN = 1结合使用以在stocks的行上运行函数的解决方案.

Here's a solution that uses apply with MARGIN = 1 to run your function over the rows of stocks.

apply(stocks, 1, function(x)

    getSymbols(Symbols = x["abbreviation"],
               from = as.Date(x["from"]),
               to = as.Date(x["to"]),
               src = "yahoo",
               env = .GlobalEnv,
               reload.Symbols = FALSE,
               verbose = FALSE,
               warnings = TRUE,
               symbol.lookup = TRUE,
               auto.assign = TRUE)

)

默认情况下,如getSymbols所做的那样,该代码在您的工作环境中创建了与所需符号相对应的新对象.

As getSymbols does by default, that code creates new objects in your working environment corresponding to the symbols you wanted.

如果要在结果数据帧上迭代其他函数,则可能需要使用lapply,使getSymbols将结果返回到一个列表,其中每个项目对应于您的一个符号.这是执行此操作的一些代码:

If you're going to iterate other functions over the resulting data frames, you'll probably want to use lapply instead, having getSymbols return the results to a list in which each item corresponds to one of your symbols. Here's some code that does that:

# lapply works best on a list, so we can use another call to lapply to create
# a list of rows from stocks
mylist <- lapply(lapply(seq(nrow(stocks)), function(i) stocks[i,]), function(x)

    # because the elements of the list we just created are data frames, we need
    # to tweak the indexing to work with column names, so we add leading commas
    getSymbols(Symbols = as.character(x[,"abbreviation"]),
               from = as.Date(x[,"from"]),
               to = as.Date(x[,"to"]),
               src = "yahoo",
               env = .GlobalEnv,
               reload.Symbols = FALSE,
               verbose = FALSE,
               warnings = TRUE,
               symbol.lookup = TRUE,
               # here's the other change, so results go to list instead of env
               auto.assign = FALSE)

)

这篇关于包含多个参数(R)的函数的前循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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