stratify: Group and count data by combinations of strata variables

Description Usage Arguments Value Examples

Description

Group data by combinations of stratum values and count the records falling into each combination.

Usage

1
2
stratify(x, strataVars = toStrataVars(x, exclude = names(x)[1]),
  column.n = "n", column.stratum = "stratum", format.stratum = "S%02d")

Arguments

x

Data frame with stratum values in columns

strataVars

List of strata variables. By default it is created from all but the first columns of x.

column.n

Column name to be used in the output for the column containing the number of records falling into the combination of stratum values.

column.stratum

Column name to be used in the output for the column containing the stratum identifier

format.stratum

format string to be used in format to create a unique id for each stratum

Value

Data frame with each row representing a combination of stratum values

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Define stratifying variables
strataVars <- list(sex = c("female", "male"), medication = c("yes", "no"))

# Create some random testdata
x <- createTestdata(strataVars)

# Count records in each stratum
stratify(x, strataVars)
stratify(x, strataVars, column.n = "n.patients")

# Create testdata with defined numbers of records in each stratum
n.per.stratum <- 1:4
y <- createTestdata(strataVars, n.per.stratum = n.per.stratum)

# Count records in each stratum
(counts <- stratify(y, strataVars))

# Check if the numbers of records in each stratum are as expected
all(counts$n == n.per.stratum)

hsonne/blockrand2 documentation built on May 17, 2019, 6:16 p.m.