在具有不同参数的数据帧中每行应用/映射不同的函数

ver*_*lcw 6 r dplyr purrr

我对你purrr专业人士在那里,一直困扰了一段时间我最好的谷歌搜索的努力一个简单的问题.首先,我们来看看我正在尝试使用的嵌套列表数据结构.

加载包

#R version 3.4.1    
library(purrr) # version 0.2.4
library(dplyr) # version 0.7.4
Run Code Online (Sandbox Code Playgroud)

定义功能

f1 <- function(a, b, c) {a + b^c}
f2 <- function(x) {x * 2}
f3 <- function(y, z) {y * z}
Run Code Online (Sandbox Code Playgroud)

定义参数集

这些都是要通过传递到每一个f1,f2以及f3:

p1 <- data_frame(a = c(2, 4, 5, 7, 8),
                 b = c(1, 1, 2, 2, 2),
                 c = c(.5, 5, 1, 2, 3))
p2 <- data_frame(x = c(1, 4))
p3 <- data_frame(y = c(2, 2, 2, 3),
                 z = c(5, 4, 3, 2))
Run Code Online (Sandbox Code Playgroud)

将它们放在一个嵌套的数据框中

我试图在一个漂亮,整洁的矩形中保持我的数据."id"变量是函数名称本身(在我的实际数据中,有数百个):

df <- data_frame(fun_id = c('f1', 'f2', 'f3'), 
                 params = list(p1, p2, p3), 
                 funs = list(f1, f2, f3))
Run Code Online (Sandbox Code Playgroud)

检查结构向我们显示列表列paramsfuns:

print(df)

# A tibble: 3 x 3
  fun_id           params   funs
   <chr>           <list> <list>
1     f1 <tibble [5 x 3]>  <fun>
2     f2 <tibble [2 x 1]>  <fun>
3     f3 <tibble [4 x 2]>  <fun>
Run Code Online (Sandbox Code Playgroud)

我的问题

使用purrr函数,或许dplyr::mutate,如何在df调用results中获得一个新的列表列,其中每个元素都是一个列表,其中包含以行方式funs从中获取参数的函数执行的输出params

我可以pmap为一个简单的案例做我想做的事:

> pmap(.l = p1, .f = f1)

[[1]]
[1] 3

[[2]]
[1] 5

[[3]]
[1] 7

[[4]]
[1] 11

[[5]]
[1] 16
Run Code Online (Sandbox Code Playgroud)

但我真的想在数据框内做这件事来保持一切顺利.以下内容将我带到了正确的结构(一个带有结果列表列的数据框),但只有一行而且没有概括:

> df %>% 
  slice(1) %>% 
  mutate(results = list(pmap(.l = params[[1]], .f = funs[[1]])))

# A tibble: 1 x 4
  fun_id           params   funs    results
   <chr>           <list> <list>     <list>
1     f1 <tibble [5 x 3]>  <fun> <list [5]>
Run Code Online (Sandbox Code Playgroud)

在此先感谢帮助解决我的问题!

PS我已经查看了以下资源,但尚未找到答案:

使用dplyr :: mutate的purrr :: pmap

在mutate中使用purrr :: pmap创建list-column

http://statwonk.com/purrr.html

https://github.com/rstudio/cheatsheets/raw/master/purrr.pdf

https://jennybc.github.io/purrr-tutorial/index.html

Cal*_*You 6

purrr正是在这种情况下具有便利功能。将功能列表应用于相应的参数列表!它被称为invoke_map并且可以与mutate以下方式一起使用。我认为,这样做的主要好处map2(~pmap())在于,如果有其他参数可以提供给未包含在params其中的任何函数,则可以将它们作为命名参数添加,...而无需修改params

library(tidyverse)
f1 <- function(a, b, c) {a + b^c}
f2 <- function(x) {x * 2}
f3 <- function(y, z) {y * z}
p1 <- data_frame(
  a = c(2, 4, 5, 7, 8),
  b = c(1, 1, 2, 2, 2),
  c = c(.5, 5, 1, 2, 3)
)
p2 <- data_frame(x = c(1, 4))
p3 <- data_frame(
  y = c(2, 2, 2, 3),
  z = c(5, 4, 3, 2)
)
df <- data_frame(
  fun_id = c("f1", "f2", "f3"),
  params = list(p1, p2, p3),
  funs = list(f1, f2, f3)
)

df2 <- df %>%
  mutate(results = invoke_map(.f = funs, .x = params))
df2
#> # A tibble: 3 x 4
#>   fun_id params           funs   results  
#>   <chr>  <list>           <list> <list>   
#> 1 f1     <tibble [5 x 3]> <fn>   <dbl [5]>
#> 2 f2     <tibble [2 x 1]> <fn>   <dbl [2]>
#> 3 f3     <tibble [4 x 2]> <fn>   <dbl [4]>
df2$results
#> [[1]]
#> [1]  3  5  7 11 16
#> 
#> [[2]]
#> [1] 2 8
#> 
#> [[3]]
#> [1] 10  8  6  6
Run Code Online (Sandbox Code Playgroud)

reprex软件包(v0.2.0)于2018-07-13创建。