簡體   English   中英

使用 purrr 從數據幀映射字符串

[英]using purrr 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嘗試返回一個空數據框。 這里有什么問題?

> 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..3..2等來引用列 #

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

要改為引用列名,我們可以使用this answer中的技巧

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM