将函数映射到两个长度不等的数据帧

Map a function to two data frames of unequal lengths

对于 df1 中的每一行,我想执行 mult 10 次,每年在 df2 中执行一次。

我能想到的一个选项是多次重复 df1 并将其加入 df2。但是我的实际数据要大得多(~20k 部分,15 个区域和 100 年),所以我正在寻找一种更有效的方法来做到这一点。

# df1

  section area         a         b         c
1       1    1 0.1208916 0.7235306 0.7652636
2       2    1 0.8265642 0.2939602 0.6491496
3       1    2 0.9101611 0.7363248 0.1509295
4       2    2 0.8807047 0.5473221 0.6748055
5       1    3 0.2343558 0.2044689 0.9647333
6       2    3 0.4112479 0.9523639 0.1533197


----------


# df2

   year         d
1     1 0.7357432
2     2 0.4591575
3     3 0.3654561
4     4 0.1996439
5     5 0.2086226
6     6 0.5628826
7     7 0.4772953
8     8 0.8474007
9     9 0.8861693
10   10 0.6694851

mult <- function(a, b, c, d) {a * b * c * d}

所需的输出看起来像这样

   section area year                 e
1        1    1    1 results of mult()
2        2    1    1 results of mult()
3        1    2    1 results of mult()
4        2    2    1 results of mult()
5        1    3    1 results of mult()
6        2    3    1 results of mult()
7        1    1    2 results of mult()
8        2    1    2 results of mult()
...

dput(df1)

structure(list(section = c(1L, 2L, 1L, 2L, 1L, 2L), area = c(1L, 
1L, 2L, 2L, 3L, 3L), a = c(0.12089157756418, 0.826564211165532, 
0.91016107192263, 0.880704707000405, 0.234355789143592, 0.411247851792723
), b = c(0.72353063733317, 0.293960151728243, 0.736324765253812, 
0.547322086291388, 0.204468948533759, 0.952363904565573), c = c(0.765263637062162, 
0.649149592733011, 0.150929539464414, 0.674805536167696, 0.964733332861215, 
0.15331974090077)), out.attrs = list(dim = structure(2:3, .Names = c("section", 
"area")), dimnames = list(section = c("section=1", "section=2"
), area = c("area=1", "area=2", "area=3"))), class = "data.frame", row.names = c(NA, 
-6L))

dput(df2)

structure(list(year = 1:10, d = c(0.735743158031255, 0.459157506935298, 
0.365456136409193, 0.199643932981417, 0.208622586680576, 0.562882597092539, 
0.477295308141038, 0.847400720929727, 0.886169332079589, 0.669485098216683
)), class = "data.frame", row.names = c(NA, -10L))

编辑:全尺寸玩具数据集

library(dplyr)

df1 <- expand.grid(section = 1:20000,
                   area = 1:15) %>%
  mutate(a = runif(300000),
         b = runif(300000),
         c = runif(300000))


df2 <- data.frame(year = 1:100,
                  d = runif(100))

您可以使用 crossing 创建 df1df2 的组合并将 mult 应用于它们。

tidyr::crossing(df1, df2) %>% dplyr::mutate(e = mult(a, b, c, d))