使用 purr 从数据帧映射字符串

using purr to map strings from dataframes

考虑这个简单的例子

testdf <- data_frame(col1 = c(2, 2),
                     col2 = c(1, 2))

# A tibble: 2 x 2
   col1  col2
  <dbl> <dbl>
1     2     1
2     2     2

然后我有另一个小标题,其中包含我想提供给 map2

的参数
mapdf <- data_frame(myinput = c('col1', 'col2'),
                    myoutput = c('col2', 'col1'))

# A tibble: 2 x 2
  myinput myoutput
  <chr>   <chr>   
1 col1    col2    
2 col2    col1 

这里是简单的函数

myfunc <- function(input, output){
  output <- sym(output)
  input <- sym(input)
    testdf %>% mutate(!!input := !!output + 1)
}

例如,在第一次迭代中,这简单地等于:

> testdf %>% mutate(col1 = col2 + 1)
# A tibble: 2 x 2
   col1  col2
  <dbl> <dbl>
1     2     1
2     3     2

但是,我 purrr 尝试在 returns 一个空数据帧下方。这里有什么问题?

> mapdf %>% map2_dfr(.$myinput, .$myoutput, myfunc(.x, .y))
# A tibble: 0 x 0

谢谢!

您可以使用 pmap

pmap(mapdf, ~ myfunc(.x, .y))

[[1]]
# A tibble: 2 x 2
   col1  col2
  <dbl> <dbl>
1     2     1
2     3     2

[[2]]
# A tibble: 2 x 2
   col1  col2
  <dbl> <dbl>
1     2     3
2     2     3

编辑 1:如评论中所建议

pmap_dfr(mapdf, ~ myfunc(.x, .y), .id = 'id')

# A tibble: 4 x 3
  id     col1  col2
  <chr> <dbl> <dbl>
1 1         2     1
2 1         3     2
3 2         2     3
4 2         2     3

编辑 2:

也可以使用 ..1..2..3

来引用第 # 列
pmap_dfr(mapdf, ~ myfunc(input = ..1, output = ..2), .id = 'id')
#> # A tibble: 4 x 3
#>   id     col1  col2
#>   <chr> <dbl> <dbl>
#> 1 1         2     1
#> 2 1         3     2
#> 3 2         2     3
#> 4 2         2     3

要改为引用列名,我们可以使用此

中的技巧
pmap_dfr(mapdf, ~ with(list(...), myfunc(myinput, myoutput)), .id = 'id')
#> # A tibble: 4 x 3
#>   id     col1  col2
#>   <chr> <dbl> <dbl>
#> 1 1         2     1
#> 2 1         3     2
#> 3 2         2     3
#> 4 2         2     3

管道将 testdf 作为第一个参数,我认为您不需要。此外,我相信如果您使用 .x.y,您将需要 ~ 来发出匿名函数信号。

> mapdf %>% {map2_dfr(.$myinput, .$myoutput, ~myfunc(.x, .y))}
# A tibble: 4 x 2
   col1  col2
  <dbl> <dbl>
1     2     1
2     3     2
3     2     3
4     2     3

也就是说,我认为您不需要匿名函数:

> mapdf %>% {map2_dfr(.$myinput, .$myoutput, myfunc)}
# A tibble: 4 x 2
   col1  col2
  <dbl> <dbl>
1     2     1
2     3     2
3     2     3
4     2     3