descfreq {mcradds} | R Documentation |
Summarize Frequency Counts and Percentages
Description
Create a summary table for one or more variables by one group, as well as a total column if necessary.
Usage
descfreq(
data,
denom = NULL,
var,
bygroup,
format,
fctdrop = FALSE,
addtot = FALSE,
na_str = NULL
)
Arguments
data |
( |
denom |
( |
var |
( |
bygroup |
( |
format |
( |
fctdrop |
( |
addtot |
( |
na_str |
( |
Value
A object Desc
contains an intermediate data with long form for
post-processing and final data with wide form for presentation.
Note
By default, the each category is sorted based on the corresponding factor
level of var
variable. If the variable is not a factor, that will be sorted
alphabetically.
Examples
data(adsl_sub)
# Count the age group by treatment with 'xx (xx.x%)' format
adsl_sub %>%
descfreq(
var = "AGEGR1",
bygroup = "TRTP",
format = "xx (xx.x%)"
)
# Count the race by treatment with 'xx (xx.xx)' format and replace NA with '0'
adsl_sub %>%
descfreq(
var = "RACE",
bygroup = "TRTP",
format = "xx (xx.xx)",
na_str = "0"
)
# Count the sex by treatment adding total column
adsl_sub %>%
descfreq(
var = "SEX",
bygroup = "TRTP",
format = "xx (xx.x%)",
addtot = TRUE
)
# Count multiple variables by treatment and sort category by corresponding factor levels
adsl_sub %>%
dplyr::mutate(
AGEGR1 = factor(AGEGR1, levels = c("<65", "65-80", ">80")),
SEX = factor(SEX, levels = c("M", "F")),
RACE = factor(RACE, levels = c(
"WHITE", "AMERICAN INDIAN OR ALASKA NATIVE",
"BLACK OR AFRICAN AMERICAN"
))
) %>%
descfreq(
var = c("AGEGR1", "SEX", "RACE"),
bygroup = "TRTP",
format = "xx (xx.x%)",
addtot = TRUE,
na_str = "0"
)