本文介绍了如何使用参数组合将 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 与长度不等的输入集一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:55