带有多个函数参数

时间:2019-04-20 08:05:36

标签: r lapply quantmod

library(quantmod)
library(xts)


getSymbols("SY1.DE", from = "2019-4-10", to = "2019-4-19", auto.assign = TRUE)
getSymbols("PEP", from = "2019-4-9", to = "2019-4-19", auto.assign = TRUE)


calcreturn <- function(data, amount = 24) {
  start <- as.numeric(data[,4][1])
  end <- as.numeric(data[,4][nrow(data)])
  difference <- end - start
  winning <- difference * amount
  return(winning)
}



allstocks <- list(SY1.DE, PEP)
amount <- list(24, 23)

lapply(allstocks, calcreturn)

大家好!

这是我的代码,用于计算我的股票收益。但是,我购买的股票数量不同,因此lapply仅在数量参数不变时起作用。有一天要处理不断变化的论点吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以修改lapply以使指数与数量一对一的股票配对运行:

lapply(1:length(allstocks), function(x) calcreturn(allstocks[[x]], amount[[x]]))