nathaneastwood / poorman

A poor man's dependency free grammar of data manipulation
https://nathaneastwood.github.io/poorman/
Other
340 stars 15 forks source link

Enable the use of {.col} and {.fn} in across() #100

Closed etiennebacher closed 2 years ago

etiennebacher commented 2 years ago

Related to #52.

Before:

library(poorman)
#> 
#>   I'd seen my father. He was a poor man, and I watched him do astonishing things.
#>     - Sidney Poitier
#> 
#> Attaching package: 'poorman'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
min_max <- list(
  min = ~min(.x, na.rm = TRUE), 
  max = ~max(.x, na.rm = TRUE)
)

dplyr::starwars %>% 
  summarise(across(where(is.numeric), min_max, .names = "{.fn}.{.col}"))
#>   X..fn....col. NA. NA..1 NA..2 NA..3 NA..4
#> 1            66 264    15  1358     8   896

Created on 2022-07-28 by the reprex package (v2.0.1)

After:

library(poorman)
#> 
#>   I'd seen my father. He was a poor man, and I watched him do astonishing things.
#>     - Sidney Poitier
#> 
#> Attaching package: 'poorman'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
min_max <- list(
  min = ~min(.x, na.rm = TRUE), 
  max = ~max(.x, na.rm = TRUE)
)

dplyr::starwars %>% 
  summarise(across(where(is.numeric), min_max, .names = "{.fn}.{.col}"))
#>   min.height max.height min.mass max.mass min.birth_year max.birth_year
#> 1         66        264       15     1358              8            896

Created on 2022-07-28 by the reprex package (v2.0.1)

Note that the function stolen from gluestick is under the MIT license, which says that "The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.". Not sure what it means but maybe you know.