Skip to contents

This funciton is a generic that includes support for dataframes and lists of dataframes. For dataframes, this function is a shortcut to dplyr::summarize(data, min = min(x), max = max(x)). For lists of dataframes that idea applies applies to each element of the list.

Usage

summarize_range(data, col, .by = NULL, na.rm = FALSE)

Arguments

data

A dataframe or a list of dataframes.

col

Unquoted expression giving the name of a column in data.

.by

A vector or list of vectors depending on whether data is a dataframe or list of dataframes, respective. For details see .by in dplyr::summarize().

na.rm

a logical indicating whether missing values should be removed.

Value

A dataframe or a list of dataframes:

  • The rows come from the underlying groups.

  • The columns come from the grouping keys plus the new columns min and max.

  • The groups are dropped.

See also

Examples

library(tibble)

data <- tibble(x = 1:4, group = letters[c(1, 1, 2, 2)])
data
#> # A tibble: 4 × 2
#>       x group
#>   <int> <chr>
#> 1     1 a    
#> 2     2 a    
#> 3     3 b    
#> 4     4 b    

summarize_range(data, "x", .by = "group")
#> # A tibble: 2 × 3
#>   group   min   max
#>   <chr> <int> <int>
#> 1 a         1     2
#> 2 b         3     4

list <- split(data, data$group)
list
#> $a
#> # A tibble: 2 × 2
#>       x group
#>   <int> <chr>
#> 1     1 a    
#> 2     2 a    
#> 
#> $b
#> # A tibble: 2 × 2
#>       x group
#>   <int> <chr>
#> 1     3 b    
#> 2     4 b    
#> 

summarize_range(list, col = "x", .by = list(a = "group", b = "group"))
#> $a
#> # A tibble: 1 × 3
#>   group   min   max
#>   <chr> <int> <int>
#> 1 a         1     2
#> 
#> $b
#> # A tibble: 1 × 3
#>   group   min   max
#>   <chr> <int> <int>
#> 1 b         3     4
#>