【发布时间】:2021-05-20 15:10:24
【问题描述】:
我正在尝试找到一种使用自定义函数运行多个模拟的方法。 每个模拟使用不同的输入值,基于现实生活中的测量。 假设这个函数可以预测大田作物的产量。
predict <- function(input1, input2, input3){
output = input1 + input2 + input3
return(output)
}
我知道如何使用 mapply 在参数列表上运行此函数,但我找不到如何使用不同的输入组合(长度不等的列表)。 为了说明,我有一个数据框(带有虚拟数字),每一行对应一个农场,每一列对应一个输入参数(除了第一个,它是农场代码)。
df <- data.frame("Farm" = 1:3, "input1" = c(10, 20 , 30), "input2" = c(100, 200, 300))
df
现在,正如您所注意到的,我在此数据框中没有第三个参数“input3”。 对于这个特定的论点,我有 1000 个不同的可能值。
all_possible_input3 <- seq(1:1000) # dummy values
我想为观察到的农场参数和 1000 个不同的可能值之间的每个组合运行预测函数。 为了展示第一个组合的几个示例,每个组合看起来像这样:
# 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。
我知道如何使用 mapply 在多个参数列表上循环一个函数,但是如何处理长度不等的列表呢? 我曾考虑在第一个函数之上添加另一个 apply 函数,但我还没有找到解决方案。 也许首先拆分数据帧,然后将每一行组合到每个可能的 input3,然后将该函数应用于每一行输入? 我希望我的例子足够清楚...... 你能帮忙吗?
【问题讨论】:
标签: r function iteration apply repeat