Skip to contents

Inspired by dplyr::vars(), but converting strings to symbols and auto names all arguments

Usage

grp(...)

Arguments

...

Variables to group by. These arguments are automatically quoted and later evaluated in the context of the data frame. They support unquoting.

Value

named list containing quosures or symbols

Examples

fn <- rOstluft.data::f("Zch_Stampfenbachstrasse_d1_2017.csv")
data <- rOstluft::read_airmo_csv(fn)

# adding group_nest to quickly glance over the groups
groupby <- function(df, group = grp()) {
  dplyr::group_by(df, !!!group) %>%
    dplyr::group_nest()
}

# no grouping -> everything will be nested
groupby(data)
#> # A tibble: 1 × 1
#>   data             
#>   <list>           
#> 1 <tibble [63 × 6]>

# use a symbol, string or an expression
groupby(data, grp(site, "unit", lubridate::year(starttime)))
#> # A tibble: 11 × 4
#>    site                    unit  `lubridate::year(starttime)`               data
#>    <fct>                   <fct>                        <dbl> <list<tibble[,4]>>
#>  1 Zch_Stampfenbachstrasse mg/m3                         2017            [3 × 4]
#>  2 Zch_Stampfenbachstrasse 1                             2017           [23 × 4]
#>  3 Zch_Stampfenbachstrasse %Hr                           2017            [3 × 4]
#>  4 Zch_Stampfenbachstrasse µg/m3                         2017           [18 × 4]
#>  5 Zch_Stampfenbachstrasse hPa                           2017            [3 × 4]
#>  6 Zch_Stampfenbachstrasse 1/cm3                         2017            [1 × 4]
#>  7 Zch_Stampfenbachstrasse min                           2017            [1 × 4]
#>  8 Zch_Stampfenbachstrasse W/m2                          2017            [2 × 4]
#>  9 Zch_Stampfenbachstrasse °C                            2017            [4 × 4]
#> 10 Zch_Stampfenbachstrasse m/s                           2017            [2 × 4]
#> 11 Zch_Stampfenbachstrasse ppb                           2017            [3 × 4]

# autonaming works fine with strings and symbols, but for expressions
# it probably a good idea to provide a name:
groupby(data, grp(site, year = lubridate::year(starttime)))
#> # A tibble: 1 × 3
#>   site                     year               data
#>   <fct>                   <dbl> <list<tibble[,5]>>
#> 1 Zch_Stampfenbachstrasse  2017           [63 × 5]