Learn R Programming

extdplyr (version 0.1.5)

pct_routine: Calculate percentage by group.

Description

pct_routine works like count except that it returns group percentages instead of counts. tally_pct is a underlying utility function that corresponds to tally. As the name implies, it also returns percentage.

Usage

pct_routine(
  data,
  ...,
  wt = NULL,
  ret_name = "pct",
  rebase = FALSE,
  ungroup = FALSE
)

pct_routine_( data, vars, wt = NULL, ret_name = "pct", rebase = FALSE, ungroup = FALSE )

tally_pct(data, wt = NULL, ret_name = "pct", rebase = FALSE)

tally_pct_(data, wt = NULL, ret_name = "pct", rebase = FALSE)

Arguments

data
...

Variables to group by, see group_by.

wt

Column name of weights.

ret_name

Character of the variable name returned.

rebase

Whether to remove the missing values in the percentage, e.g. rebase the percentage so that NAs in the last group are excluded.

ungroup

Whether to ungroup the returned table.

vars

A character vector of variable names to group by.

Functions

  • pct_routine_: SE version of pct_routine.

  • tally_pct: NSE version of tally_pct_.

  • tally_pct_: Underlying SE function of pct_routine_ without options for groups.

Examples

Run this code
# NOT RUN {
data(esoph)
esoph
pct_routine(esoph, agegp, alcgp)
pct_routine(esoph, agegp, alcgp, wt = ncases)
# Crate new grouping variables
pct_routine(esoph, agegp, low_alcgp = alcgp %in% c("0-39g/day", "40-79"))


# This examples shows how rebase works
if (require(dplyr)) {
  iris %>%
    mutate(random_missing = ifelse(rnorm(n()) > 0, NA, round(Sepal.Length))) %>%
    group_by(Species, random_missing) %>%
    tally_pct(wt = Sepal.Width, rebase = TRUE)
}
# }

Run the code above in your browser using DataLab