如何使用参数的组合将mapply与长度不等的输入集一起使用? [英] How to use mapply with sets of inputs of unequal lengths, using combinations of arguments?

查看:65
本文介绍了如何使用参数的组合将mapply与长度不等的输入集一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种使用自定义函数运行多个模拟的方法.每个模拟基于实际测量使用不同的输入值.假设这个函数可以预测大田作物的产量.

I am trying to find a way to run multiple simulations using a custom function. Each simulation uses different input values, based on real-life measurements. Let's say this function predicts the yield of a field crop.

predict <- function(input1, input2, input3){
   output = input1 + input2 + input3
   return(output)
   }

我知道如何使用 mapply 在参数列表上运行此函数,但是我找不到如何使用输入的不同组合(长度不等的列表)的方法.为了说明这一点,我有一个数据帧(带有虚拟数字),每一行对应一个服务器场,每一列对应一个输入参数(除了第一个参数,即服务器场代码).

I know how to use mapply to run this function over lists of arguments, but I cannot find how to use different combinations of inputs (lists of unequal lengths). To illustrate, I have a dataframe (with dummy numbers), with each row corresponding to a farm, each column corresponding to an input argument (apart from the first, which is the farm code).

df <- data.frame("Farm" = 1:3, "input1" = c(10, 20 , 30), "input2" = c(100, 200, 300))
df

现在,您已经注意到,我没有第三个参数"input3",在此数据框中.对于这个特定的参数,我有1000个不同的可能值.

Now, as you have noticed, I don't have the third argument "input3" in this dataframe. For this particular argument, I have 1000 different possible values.

all_possible_input3 <- seq(1:1000) # dummy values

我想为观察到的场参数和1000个不同的可能值之间的每种组合运行预测函数.为了显示第一个组合的一些示例,每个人都看起来像这样:

I want to run the predict function for each combination between the observed farm parameters and the 1000 different possible values. To show a few examples of the first combinations, each individual would look like this:

# For Farm 1:
Farm1_run1 <- predict(input1 = 10, input2 = 100, input3 = 1)
Farm1_run2 <- predict(input1 = 10, input2 = 100, input3 = 2)
Farm1_run3 <- predict(input1 = 10, input2 = 100, input3 = 3)
# ... and goes on to use the 1000 values for the third argument.

# For Farm 2:
Farm2_run1 <- predict(input1 = 20, input2 = 200, input3 = 1)
Farm2_run2 <- predict(input1 = 20, input2 = 200, input3 = 2)
Farm2_run3 <- predict(input1 = 20, input2 = 200, input3 = 3)
# ... and goes on to use the 1000 values for the third argument.

# For Farm 3:
Farm3_run1 <- predict(input1 = 30, input2 = 300, input3 = 1)
Farm3_run2 <- predict(input1 = 30, input2 = 300, input3 = 2)
Farm3_run3 <- predict(input1 = 30, input2 = 300, input3 = 3)
# ... and goes on to use the 1000 values for the third argument.

总共应该产生3000个运行,对应于3个场和1000个可能的输入3之间的所有组合.

In total, that should produce 3000 runs, corresponding to all combinations between the 3 farms and the 1000 possible input3.

我知道如何使用mapply在多个参数列表上循环一个函数,但是如何处理长度不等的列表?我曾考虑在第一个功能上层叠另一个 apply 功能,但是我还没有找到解决方案.也许首先拆分数据帧,然后将每一行组合到每个可能的 input3 ,然后将函数应用于输入的每一行?我希望我的例子足够清楚...你能帮忙吗?

I know how to use mapply to loop a function over multiple lists of arguments, but how to deal with lists of unequal lengths? I was think of layering another apply function over a first one, but I have not yet found a solution. Perhaps splitting first the dataframe, then combining each row to each possible input3, and then apply the function to each row of inputs? I hope my example is clear enough... Could you help?

推荐答案

考虑第二遍申请,您走在正确的轨道上.您可以例如将您的 mapply 嵌套在 lapply 内,该循环遍历您的 all_possible_input3 :

You were on the right track by considering a second apply. You could e.g. nest your mapply inside a lapply which loops over your all_possible_input3:

predict <- function(input1, input2, input3){
  output = input1 + input2 + input3
  return(output)
}

df <- data.frame("Farm" = 1:3, "input1" = c(10, 20 , 30), "input2" = c(100, 200, 300))
df
#>   Farm input1 input2
#> 1    1     10    100
#> 2    2     20    200
#> 3    3     30    300

all_possible_input3 <- 1:10

farm_runs <- lapply(all_possible_input3, function(input3) {
  mapply(predict, input1 = df$input1, input2 = df$input2, input3 = input3)
})
farm_runs
#> [[1]]
#> [1] 111 221 331
#> 
#> [[2]]
#> [1] 112 222 332
#> 
#> [[3]]
#> [1] 113 223 333
#> 
#> [[4]]
#> [1] 114 224 334
#> 
#> [[5]]
#> [1] 115 225 335
#> 
#> [[6]]
#> [1] 116 226 336
#> 
#> [[7]]
#> [1] 117 227 337
#> 
#> [[8]]
#> [1] 118 228 338
#> 
#> [[9]]
#> [1] 119 229 339
#> 
#> [[10]]
#> [1] 120 230 340

这篇关于如何使用参数的组合将mapply与长度不等的输入集一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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