groupwiseCount | R Documentation |
Performs a 2 level count either preserving the structure of the dataframe (e.g. as a mutate function) or as a summary, returning the dataframe with total counts for the grouping (N) and for the subgroup defined by "groupVars" (N_x). This also allows us to calculate a probability of the subgroup in the group. This is different from an rank_percent in that the input datafram may have already been summarised
groupwiseCount(df, groupVars, countVar = NULL, summarise = FALSE)
df |
- a df which may be grouped. Grouping typically will be on a feature. N is the count of the items in the group |
groupVars |
- the grouping for which we want to create a label as a list of columns quoted by vars(...). This could be an outcome and |
countVar |
- optional: the datatable column containing the observed frequency of the event X. If this is missing the row count will be used instead (i.e. assumes each row is an observation). |
summarise |
- return dataframe as-is with additional columns (N, N_x, p_x) (FALSE - the default) or return dataframe as group summary with only grouping info and output (TRUE) |
the grouped dataframe containing at a minumum, the df grouping columns, the groupVars columns, and a groupwise count of both levels of grouping labelled N and N_x and the groupwise p_x.
mtcars %>% group_by(cyl) %>% groupwiseCount(vars(gear), summarise=TRUE) %>% mutate(p_x = N_x/N)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.