aggr_neat | R Documentation |
Returns aggregated values per group for given variable. Serves
as argument in the table_neat
function.
aggr_neat( dat, values, method = mean, group_by = NULL, filt = NULL, sep = "_", prefix = NULL, new_name = NULL, round_to = 2 )
dat |
A data frame (or a |
values |
The vector of numbers from which the statistics are to be
calculated, or the name of the column in the |
method |
Function of string. If function, uses the |
group_by |
String, or vector of strings: the name(s) of the column(s) in
the |
filt |
An expression to filter, by column values, the entire |
sep |
String (underscore |
prefix |
|
new_name |
|
round_to |
Number of digits after the decimal point to round to, when
using |
A data.table
with the statistics per group, with a
single column ("aggr_group"
) indicating the grouping.
table_neat
to create full tables using multiple
variables
data("mtcars") # load base R example dataset # overall means and SDs for wt (Weight) aggr_neat(mtcars, wt) # rename column aggr_neat(mtcars, wt, new_name = 'weight') # grouped by cyl (Number of cylinders) aggr_neat(mtcars, wt, group_by = 'cyl') # grouped by cyl and gear aggr_neat(mtcars, wt, group_by = c('cyl', 'gear')) # prefix for group names aggr_neat(mtcars, wt, group_by = 'cyl', prefix = 'cyl') # filter to only have cyl larger than 4 aggr_neat(mtcars, wt, group_by = 'cyl', filt = cyl > 4) # filter to only have hp (Gross horsepower) smaller than 200 aggr_neat(mtcars, wt, group_by = 'cyl', filt = hp < 200) # combine two filters above, and add prefix aggr_neat( mtcars, wt, group_by = 'cyl', filt = (hp < 200 & cyl > 4), prefix = 'filtered' ) # add SD (and round output numbers to 2) aggr_neat(mtcars, wt, group_by = 'cyl', method = 'mean+sd', round_to = 2) # now medians instead of means aggr_neat(mtcars, wt, group_by = 'cyl', method = median) # with SD aggr_neat(mtcars, wt, group_by = 'cyl', method = 'median+sd', round_to = 1) # overall ratio of gear 4 (Number of gears) aggr_neat(mtcars, gear, method = '4') # overall ratio of gear 4 and 5 aggr_neat(mtcars, gear, method = '4, 5') # same ratio calculated per each cyl aggr_neat(mtcars, gear, group_by = 'cyl', method = '4, 5') # per each cyl and per vs (engine type) aggr_neat(mtcars, gear, group_by = c('cyl', 'vs'), method = '4, 5') # ratio of gear 3 per gear 3 and 5 aggr_neat( mtcars, gear, group_by = 'cyl', method = '3', filt = gear %in% c(3, 5) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.