calc_cat_freqs: Calculate frequencies and proportions for a categorical or...

Description Usage Arguments Value See Also Examples

View source: R/calc_cat_freqs.R

Description

This function was inspired by Hmisc::describe, but adding the flexibility to use a vector of denominators vs length(non-missing values) to calculate proportions. For example, we might have a multicenter study and want the proportion of patients excluded out of those enrolled per site.

Usage

1
2
calc_cat_freqs(descVar, allLevels, overall = TRUE, changeDenom = FALSE,
  useDenom, pctString = FALSE)

Arguments

descVar

Vector to be described.

allLevels

Vector with all levels which should be present. Defaults to unique(descVar).

overall

Logical; whether to provide overall count and percent.

changeDenom

Logical; whether to use a different denominator than length(descVar).

useDenom

Numeric vector (length = length(allLevels)); denominators to be used if changeDenom = TRUE.

pctString

Logical; whether to include a second element of strings of form "N (Pct)".

Value

List with 1 or 2 elements: always $freqNums, and $freqStrings if pctString = TRUE.

See Also

describe.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
## Create example vector
x <- sample(LETTERS[1:5], size = 50, replace = TRUE)

## Calculate frequencies and proportions using defaults
calc_cat_freqs(x, pctString = TRUE)

## But maybe we had possibilities for F and G levels too, they just don't show up
calc_cat_freqs(x, allLevels = LETTERS[1:7])

## Alternatively, maybe we don't care about E
calc_cat_freqs(x, allLevels = LETTERS[1:4])

## But above gives us % of total non-missing values of descVar; we want the total of just A:D
calc_cat_freqs(x, allLevels = LETTERS[1:4], changeDenom = TRUE, useDenom = sum(x %in% LETTERS[1:4]))

## Maybe A-E are sites and x represents a patient excluded from each; we want the denominator to
## be number screened at each site
## Let us pretend this is the number screened:
nScreened <- c(100, 110, 120, 130, 140)
calc_cat_freqs(x, changeDenom = TRUE, useDenom = nScreened)

jenniferthompson/JTHelpers documentation built on May 19, 2019, 4:04 a.m.