summary_row: summary_row

Description Usage Arguments Examples

View source: R/summary-row.R

Description

add a summary row to a data frame

Usage

1
2
summary_row(df, cols, fun, ..., rows = NA, label_col = NA,
  label = "Summary", new_row = TRUE, fill = NA)

Arguments

df

the data frame

cols

numeric vector of columns to include in the summary row

fun

function to summarize data frame rows

...

other arguments to be passed to the function specified in the fun argument

rows

numeric vector of rows to include in summary calculation. By default rows is set to all rows that were not created by the a summary_row function.

label_col

column number to hold the summary row label specified by the label argument

label

summary row label (e.g. "Totals")

new_row

boolean value indicating whether the summary row should be a new row attached to the bottom of the data frame or if it should be combined with an already existing summary row.

fill

value to insert in columns not specified in the cols argument. Default is NA

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
df <- data.frame("year" = 2014:2016,
                 "a" = c(NA, 1, 3),
                 "b" = c("g", "e", "f")
      )

summary_row(fun = sum, na.rm = TRUE, df = df, cols = 2)
summary_row(fun = sum, na.rm = TRUE, df = df, cols = 2, label_col = 1, 
            label = "Totals")
summary_row(fun = sum, na.rm = TRUE, df = df, cols = 2, fill = "HI")

df$d <- 5:7

# add new row
df <- summary_row(fun = sum, na.rm = TRUE, df = df, cols = c(2, 4), 
                  label_col = 1, label = "Totals")

# adjust newest row
df <- summary_row(fun = mean, na.rm = TRUE, df = df, cols = c(2, 4), 
                  label_col = 1, label = "Averages")

# I didn't want the mean in column 4, I actually wanted the product
df <- summary_row(fun = prod, na.rm = TRUE, df = df, cols = 4, label_col = 1, 
                  label = "Averages/Products", new_row = FALSE)
                  
# new cummary row with the average of the last 2 years
summary_row(fun = mean, df = df, cols = c(2, 4), rows = 2:3, label_col = 1, 
            label = "2 Yr AVG")

merlinoa/summaryrow documentation built on May 22, 2019, 6:53 p.m.