stratifiedNorm: Function to draw observations from stratified norm sample

Description Usage Arguments Examples

View source: R/stratifiedNorm.R

Description

This function creates a stratified random sample, given an arbitrary number of factors and levels within those factors. Credits to Matthew Sigal: https://gist.github.com/mattsigal/c17650d8a9b0f5b018af

Usage

1
2
stratifiedNorm(dat, strata, observations = 0, return.grid = FALSE,
  full.data = FALSE, full.data.id = "sampled")

Arguments

dat

data.frame object

strata

character; a named vector indicating the strata variables

observations

numeric; number of cases to sample from each strata

return.grid

logical; return grid of factor combintions?

full.data

logical; return the original dataset with a new logical variable (named from 'full.data.id') indicating whether the row was selected

full.data.id;

a string pertaining the variable name used when 'full.data=TRUE'

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#' \dontrun{
dat <- data.frame(Gender=sample(c('Male', 'Female'), size = 1500, replace = TRUE),
               AgeGroup=sample(c('18-39', '40-49', '50+'), size = 1500, replace = TRUE),
               Relationship=sample(c('Direct', 'Manager', 'Coworker', 'Friend'), size = 1500, replace = TRUE),
               X=rnorm(n=1500, mean=0, sd=1),
               Y=rnorm(n=1500, mean=0, sd=1),
               Z=rnorm(n=1500, mean=0, sd=1))

strata = c('Gender', 'AgeGroup', 'Relationship')

stratifiedNorm(dat, strata, return.grid=TRUE)
test <- stratifiedNorm(dat, strata, counts = 5, return.grid=FALSE)
head(test)

stratifiedNorm(dat, strata, counts = 5, full.data=TRUE)
}

MHS-R/mhs documentation built on May 25, 2019, 12:23 p.m.