【问题标题】:Using mapply where the arguments are specified by a data frame在参数由数据框指定的地方使用 mapply
【发布时间】:2020-07-28 23:13:19
【问题描述】:

我试图多次使用一个函数并且每次都改变参数。我想改变的论点也会改变。

我正在尝试使用列名指定我要更改的参数的数据框。

例如,假设我们使用mapply 将 1:3 乘以 4 和 5:

f <- function(A, B, C = 1) A * B * C
mapply(f, A = rep(1:3, 2), B = rep(4:5, each = 3))

我们可以使用expand.grid 让事情变得更简单:

arg <- expand.grid(A = 1:3, B = 4:5)
mapply(f, A = arg$A, B = arg$B)

我正在尝试这样做:

mapply(f, arg)

这样参数AB 在输入arg 中指定。这可能吗?

【问题讨论】:

    标签: r apply mapply


    【解决方案1】:

    我们可以使用do.call

    do.call(Map, c(f = f, arg))
    

    mapply

    do.call(mapply, c(FUN = f, arg))
    #[1]  4  8 12  5 10 15
    

    【讨论】:

      猜你喜欢
      • 2016-09-23
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      相关资源
      最近更新 更多