Skip to contents

[Experimental]

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

(data.frame)
a data frame that contains the variables to be summarized and grouped.

denom

(data.frame)
the denominator to use for the percentage, but not use temporarily. By default, it's NULL, meaning the function will use the number of values of the data, including missing value.

var

(vector)
a character vector of variables to be summarized within data.

bygroup

(string)
a character variable for grouping within data.

format

(string)
formatting string from formatters::list_valid_format_labels() for frequency counts and percentages.

fctdrop

(logical)
whether to include the levels of the variables but with no records.

addtot

(logical)
whether to add total column in the output or not.

na_str

(string)
a string to replace NA in the output if no records will be counted for any category.

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%)"
  )
#> Error in as.list.default(X): no method for coercing this S4 class to a vector

# 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"
  )
#> Error in as.list.default(X): no method for coercing this S4 class to a vector

# Count the sex by treatment adding total column
adsl_sub %>%
  descfreq(
    var = "SEX",
    bygroup = "TRTP",
    format = "xx (xx.x%)",
    addtot = TRUE
  )
#> Error in as.list.default(X): no method for coercing this S4 class to a vector

# 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"
  )
#> Error in as.list.default(X): no method for coercing this S4 class to a vector