freq_two_vects: freq_two_vects

Description Usage Arguments Details Value Examples

Description

freq_two_vects takes two columns from a data frame and returns a data frame containing frequency tables with counts and percentages of col2 within col1. It answers the question, what percent of col1 is col2.

Usage

1
freq_two_vects(df, col1, col2, separate_tables = FALSE)

Arguments

df

a data frame

col1

a column from the data frame to be aggregated at the higher level.

col2

a column from the data frame to be aggregated within col1.

separate_tables

a boolean that decides if you want all aggregations in one data frame or split apart so each element of col1 gets it's own table; the default is FALSE

Details

As an example, if col1 was gender and col2 was ethnicity you would get the count and rate of males that are asian, african american, hispanic, etc.

If col1 was ethnicity and col2 was gender you would get the count and rate of asians that are male and female.

Value

returns a list containing frequency tables split by col1_string with counts and rates of col2_string.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Sample data frame to demo the freq_two_vects function.
df <- data.frame(gender = sample(c('m','f'), 200, replace = TRUE),
                 ethnicity = sample(c('african american', 'asian', 'caucasian',
                                   'hispanic', 'other'),
                                   200, replace = TRUE),
                 stringsAsFactors = FALSE)

freq_two_vects(df, gender, ethnicity, FALSE)
gender_by_ethnicity <- freq_two_vects(df, gender, ethnicity, TRUE)
gender_by_ethnicity$m
freq_two_vects(df, gender, ethnicity, TRUE)$m
freq_two_vects(df, ethnicity, gender, FALSE)
ethnicity_by_gender <- freq_two_vects(df, ethnicity, gender, TRUE)
ethnicity_by_gender$asian

DataInsightPartners/frequencies documentation built on May 8, 2019, 9:57 p.m.