purrr icon indicating copy to clipboard operation
purrr copied to clipboard

feature request: .name_repair for map_dfc()

Open LukaszDerylo opened this issue 5 years ago • 1 comments

It would be nice to have greater control of how columns are named after mapping (with map_dfc() ) a function that itself returns a data frame. Toy example below. It would be also nice to have a way to silence "New names" message, as decribed here: tibble/#632. Including .name_repair argument would, I think, do the thing.

foo<-function(sd){
  data.frame(
    first=rnorm(2,0,sd),
    second=rnorm(2,0,sd/2)
  )
}


map_dfc(1:2, foo)
New names:
* first -> first...1
* second -> second...2
* first -> first...3
* second -> second...4
   first...1  second...2 first...3 second...4
1 -0,4667700  0,07934606 -1,835292 -1,0368622
2 -0,8030472 -0,63820208 -2,305742 -0,8487043

LukaszDerylo avatar Jun 04 '20 07:06 LukaszDerylo

Is it possible to also expose .name_repair in the *_dfc family of functions in purrr, so that it would simplify the following for suppressing the new names message:

library(purrr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(vctrs)
#> 
#> Attaching package: 'vctrs'
#> The following object is masked from 'package:dplyr':
#> 
#>     data_frame

tmp1 <- iris %>%
  select(-Species) %>%
  cbind(-.) %>%
  map_dfc(~ ifelse(.x < 0, 0, .x))
#> New names:
#> * Sepal.Length -> Sepal.Length...1
#> * Sepal.Width -> Sepal.Width...2
#> * Petal.Length -> Petal.Length...3
#> * Petal.Width -> Petal.Width...4
#> * Sepal.Length -> Sepal.Length...5
#> * ...

tmp2 <- iris %>%
  select(-Species) %>%
  cbind(-.) %>%
  map(~ ifelse(.x < 0, 0, .x)) %>%
  bind_cols(.name_repair = ~ vec_as_names(..., repair = "unique", quiet = TRUE))

identical(tmp1, tmp2)
#> [1] TRUE

Created on 2021-05-20 by the reprex package (v2.0.0)

dchiu911 avatar May 20 '21 23:05 dchiu911

We'll do this as part of #758.

hadley avatar Aug 27 '22 12:08 hadley