Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let .id arg in map_dfr specify values as well as name #791

Closed
gregorp opened this issue Aug 26, 2020 · 3 comments
Closed

Let .id arg in map_dfr specify values as well as name #791

gregorp opened this issue Aug 26, 2020 · 3 comments

Comments

@gregorp
Copy link

gregorp commented Aug 26, 2020

I'd like to be able to add an ID column to the output of map_dfr that's not defined by the names of the result, but rather is specified in the .id argument.

Inspired by this Stack Overflow question, where the OP wants to get the min and max of each column of a data frame, and have the results, in rows, with a column labeling the min and max row.

library(purrr)
library(dplyr, warn.conflicts = FALSE)

## this is the idea - but I wish we didn't need the `mutate`
map_dfr(mtcars, ~c(min(.), max(.))) %>% 
  mutate(stat = c("min", "max")) %>% 
  select(stat, everything())
#> # A tibble: 2 x 12
#>   stat    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 min    10.4     4  71.1    52  2.76  1.51  14.5     0     0     3     1
#> 2 max    33.9     8 472     335  4.93  5.42  22.9     1     1     5     8

## I would love if a named list given to `.id` would add values 
## unfortunately, the .id argument seems to be silently ignored in this case
map_dfr(mtcars, ~c(min(.), max(.)), .id = list(stat = c("min", "max")))
#> # A tibble: 2 x 11
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1  10.4     4  71.1    52  2.76  1.51  14.5     0     0     3     1
#> 2  33.9     8 472     335  4.93  5.42  22.9     1     1     5     8

## This surprised me - if `.f` returns a named vector, the output is long, not wide. Undocumented feature?
map_dfr(mtcars, ~c("min" = min(.), "max" = max(.)))
#> # A tibble: 11 x 2
#>      min    max
#>    <dbl>  <dbl>
#>  1 10.4   33.9 
#>  2  4      8   
#>  3 71.1  472   
#>  4 52    335   
#>  5  2.76   4.93
#>  6  1.51   5.42
#>  7 14.5   22.9 
#>  8  0      1   
#>  9  0      1   
#> 10  3      5   
#> 11  1      8

## When `.f` returns a named vector, the `.id` arg is necessary to make sense of the output
map_dfr(mtcars, ~c("min" = min(.), "max" = max(.)), .id = "stat")
#> # A tibble: 11 x 3
#>    stat    min    max
#>    <chr> <dbl>  <dbl>
#>  1 mpg   10.4   33.9 
#>  2 cyl    4      8   
#>  3 disp  71.1  472   
#>  4 hp    52    335   
#>  5 drat   2.76   4.93
#>  6 wt     1.51   5.42
#>  7 qsec  14.5   22.9 
#>  8 vs     0      1   
#>  9 am     0      1   
#> 10 gear   3      5   
#> 11 carb   1      8

Created on 2020-08-26 by the reprex package (v0.3.0)

I think it would be intuitive and useful if specifying .id = list(stat = c("min", "max")) created a column named stat taking on values "min" "max".

@gregorp
Copy link
Author

gregorp commented Aug 26, 2020

Seems somewhat related to #472 .

@jennybc
Copy link
Member

jennybc commented Aug 26, 2020

This starts to feel a bit like code golf, in the sense that I'm not sure how easy it is to revisit and modify this code. But here is one more way to get what you want:

library(tidyverse)

list(min = min, max = max) %>% 
  map_dfr(~ map_dfc(mtcars, .x), .id = "stat")
#> # A tibble: 2 x 12
#>   stat    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 min    10.4     4  71.1    52  2.76  1.51  14.5     0     0     3     1
#> 2 max    33.9     8 472     335  4.93  5.42  22.9     1     1     5     8

Created on 2020-08-26 by the reprex package (v0.3.0.9001)

If you want one row per summary stat, then what you're really mapping over is min() and max().

@hadley
Copy link
Member

hadley commented Aug 24, 2022

The convention of using .id to extract column values from names is now implemented pretty deeply in vctrs, and I don't think we want to expand that convention at the moment.

@hadley hadley closed this as completed Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants