ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19375

using purrr to map strings from dataframes

Consider this simple example

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

Then I have another tibble, which contains the arguments I want to feed to 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 

And here is the simple function

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

For instance, at the first iteration this is simply equal to:

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

However, my purrr attempt below returns an empty dataframe. What is the issue here?

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

Thanks!

Upvotes: 2

Views: 399

Answers (2)

Tung
Tung

Reputation: 28331

You can use 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

Edit 1: as suggested in the comment

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

Edit 2:

Can also refer to column # by using ..1, ..2, ..3, etc

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

To refer to column names instead, we can use the trick from 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

Upvotes: 2

zack
zack

Reputation: 5405

The pipe is piping the testdf as the first argument, which I don't think you want. Also, I believe you'll need ~ to signal an anonymous function if you're using .x and .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

That said, I don't think you need the anonymous function:

> 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

Upvotes: 1

Related Questions