Learn R Programming

percentiles (version 0.2.2)

calculate_stratified_percentiles: Calculate stratified percentiles

Description

Calculate percentiles for values in a data.frame while stratifying for other characteritics in same df

Usage

calculate_stratified_percentiles(data, value_col, stratify_by, use.na = FALSE)

Arguments

data

A data frame

value_col

character name of column containing values

stratify_by

list or vector. Use a named list to specify column name as key and a value of type vector indicating accepted levels of the property stratified by to be included. If an unnamed list or vector is passed, all levels of indicated columns will be used

use.na

A logical indicating whether NA values should be used. If TRUE, NA values and non-included value levels will be grouped like a separate value level

Value

A vector of numerics with percentile values of length of nrow(data)

Examples

Run this code
# NOT RUN {
data <- data.frame('values' = 100:1, 'group' = rep(c('A', 'B', NA, 'D'), 25))
calculate_stratified_percentiles(data, 'values', list(group = c('A', 'B', 'D')))
calculate_stratified_percentiles(data, 'values', c('group'), use.na = TRUE)
calculate_stratified_percentiles(data, 'values', list(group = c('A', 'C')), use.na=TRUE)
# The following example will result in NA values caused by NAs in 'group'.
# Therefore, it will return the percentile vector, but issue a warning.
# }
# NOT RUN {
calculate_stratified_percentiles(data, 'values', 'group')
# }
# NOT RUN {

# }

Run the code above in your browser using DataLab