Add margins to a data frame.
Rownames are silently stripped. All margining variables will be converted to factors.
add_margins(df, vars, margins = TRUE)
input data frame
a list of character vectors giving the variables in each dimension
a character vector of variable names to compute margins for.
TRUEwill compute all possible margins.
#1 outputs average mpg based on cylinder mtcars %>% group_by(cyl) %>% summarize(AVG_MPG = mean(mpg)) #2 output same as above but adds an (all) row which has the average mpg for all cars in data frame mtcars %>% reshape2::add_margins("cyl") %>% group_by(cyl) %>% summarize(AVG_MPG = mean(mpg))