Add row and/or column summaries (e.g., total counts) to a data frame.

append_summary(df, ..., row = TRUE, col = TRUE, .f = sum, args = NULL)

Arguments

df

A data frame to append summaries to.

...

Unquoted names of columns to be included in the summary

row

logical indicating whether a summary row should be added (i.e., summarizing each column)

col

logical indicating whether a summary column should be added (i.e., summarizing each row)

.f

Function to use for calculating summaries

args

A named list of arguments to pass to .f

Value

A data frame with the summary row and/or column appended

Examples

set.seed(9416)
df <- tibble::tibble(char = letters[1:5], x = rnorm(5), y = rnorm(5))
append_summary(df, x, y, row = TRUE, col = TRUE, .f = sum)
#> # A tibble: 6 × 4
#>   char       x      y    sum
#>   <chr>  <dbl>  <dbl>  <dbl>
#> 1 a     -0.743 -1.28  -2.02 
#> 2 b      0.205 -0.900 -0.695
#> 3 c     -0.724  1.54   0.811
#> 4 d      0.319  1.40   1.72 
#> 5 e      0.141 -0.645 -0.504
#> 6 NA    -0.802  0.116 -0.686
append_summary(df, x, y, row = FALSE, .f = mean)
#> # A tibble: 5 × 4
#>   char       x      y   mean
#>   <chr>  <dbl>  <dbl>  <dbl>
#> 1 a     -0.743 -1.28  -1.01 
#> 2 b      0.205 -0.900 -0.348
#> 3 c     -0.724  1.54   0.406
#> 4 d      0.319  1.40   0.861
#> 5 e      0.141 -0.645 -0.252