larmarange / broom.helpers

A set of functions to facilitate manipulation of tibbles produced by broom
https://larmarange.github.io/broom.helpers/
GNU General Public License v3.0
22 stars 7 forks source link

Should `tidy_select_variables()` rearrange the order of the variables according to `include`? #183

Closed larmarange closed 2 years ago

larmarange commented 2 years ago

Currently, the include parameter preserves the original order of the variables.

library(broom.helpers)

mod <- lm(Sepal.Length ~ ., data = iris)

mod |> 
  tidy_plus_plus() |> 
  dplyr::select(term, variable, label, estimate)
#> # A tibble: 6 × 4
#>   term              variable     label        estimate
#>   <chr>             <chr>        <chr>           <dbl>
#> 1 Sepal.Width       Sepal.Width  Sepal.Width     0.496
#> 2 Petal.Length      Petal.Length Petal.Length    0.829
#> 3 Petal.Width       Petal.Width  Petal.Width    -0.315
#> 4 Speciessetosa     Species      setosa          0    
#> 5 Speciesversicolor Species      versicolor     -0.724
#> 6 Speciesvirginica  Species      virginica      -1.02

mod |> 
  tidy_plus_plus(include = c(Species, Petal.Length)) |> 
  dplyr::select(term, variable, label, estimate)
#> # A tibble: 4 × 4
#>   term              variable     label        estimate
#>   <chr>             <chr>        <chr>           <dbl>
#> 1 Petal.Length      Petal.Length Petal.Length    0.829
#> 2 Speciessetosa     Species      setosa          0    
#> 3 Speciesversicolor Species      versicolor     -0.724
#> 4 Speciesvirginica  Species      virginica      -1.02

mod |> 
  tidy_plus_plus(include = c(Species, Petal.Length, everything())) |> 
  dplyr::select(term, variable, label, estimate)
#> # A tibble: 6 × 4
#>   term              variable     label        estimate
#>   <chr>             <chr>        <chr>           <dbl>
#> 1 Sepal.Width       Sepal.Width  Sepal.Width     0.496
#> 2 Petal.Length      Petal.Length Petal.Length    0.829
#> 3 Petal.Width       Petal.Width  Petal.Width    -0.315
#> 4 Speciessetosa     Species      setosa          0    
#> 5 Speciesversicolor Species      versicolor     -0.724
#> 6 Speciesvirginica  Species      virginica      -1.02

Created on 2022-10-07 with reprex v2.0.2

To be consistent with dplyr::select(), should the table be reordered according to the include parameters?

@ddsjoberg do you have an advice about it?

ddsjoberg commented 2 years ago

Oh good point. I think it would be preferable to have the order adjusted with include= if it's not too hairy to make happen.

larmarange commented 2 years ago

I will explore it

larmarange commented 2 years ago

It is finally not too complicated to do it. See #184