Skip to contents

groupedTable() creates a summary table of frequencies and percentages that can show breakdowns by groups and the totals for the data passed to it. The table shows each item and all the responses by row.

Usage

groupedTable(df, col_group = NULL, question_labels = NULL, str_width = 20)

Arguments

df

Required, a tibble or data frame of categorical/factor data that also can contain a categorical group variable to split up the data, e.g. role, gender, education level, etc.

col_group

Default is NULL. The name of the categorical group variable to split up the data, e.g. role, gender, education level, etc. Must be in quotes (e.g. "role").

question_labels

Default is NULL. Takes in a named character vector to both supply labels the questions and sort the order of the questions.The named character vector should have the new labels as the "name" and the old labels as the "variable" sorted in the desired order of appearing in the table, first item will appear at the top of the table. See examples.

str_width

Default is 20. The character length to wrap the question column. If question_labels are supplied and very long use this to keep the question column from being too large for the table.

Value

a flextable object with columns for question, response, and counts and percentages for each group and a total column. Colors are set to The Mark USA branding

Examples

 data <- dplyr::tibble(
  Cohort = factor(c(1,2,1,2,1,2,1,2,1,2,1,2), levels = c(1, 2)),
  gender = factor(c(
             "Female", "Female","Female","Male", "Female","Male",
             "Male", "Female","Male", "Female", "Male", "Female"
             ), levels = c("Female", "Male")),
  year = factor(c(
      "grad", "undergrad", "grad", "undergrad", "grad","undergrad",
      "undergrad", "undergrad", "grad", "undergrad", "grad","undergrad"
                ), levels = c("grad", "undergrad")),
 department = factor(c(
      "Chemistry", "Biology", "Chemistry", "Biology", "Physics","Biology",
      "Biology", "Physics", "Chemistry", "Chemistry", "Physics","Biology"
               ), levels = c("Biology", "Chemistry", "Physics")),
 ethnicity = factor(c(
      "Asian", "Black", "white", "Hispanic or Latino", "white","Asian",
      "Black", "Asian", "white", "white", "Hispanic or Latino","Hispanic or Latino"
                  ), levels = c( "Asian", "Black", "Hispanic or Latino", "white"))
)

# Labels for questions column of table, pass to question_labels argument:
labels <- c('Gender' = "gender",
             'Ethnicity' = "ethnicity",
             'Year in school' = "year",
             'Department of Affiliation' = "department")

# Call groupedTable with a grouping variable:
data %>% groupedTable(col_group = "Cohort", question_labels = labels)

Question

Response

1
(n = 6)

2
(n = 6)

Total
(n = 12)1

Gender

Female

3 (25%)

4 (33%)

7 (58%)

Male

3 (25%)

2 (17%)

5 (42%)

Ethnicity

Asian

1 (8%)

2 (17%)

3 (25%)

Black

1 (8%)

1 (8%)

2 (17%)

Hispanic or Latino

1 (8%)

2 (17%)

3 (25%)

white

3 (25%)

1 (8%)

4 (33%)

Year in school

grad

5 (42%)

-

5 (42%)

undergrad

1 (8%)

6 (50%)

7 (58%)

Department of
Affiliation

Biology

1 (8%)

4 (33%)

5 (42%)

Chemistry

3 (25%)

1 (8%)

4 (33%)

Physics

2 (17%)

1 (8%)

3 (25%)

1n (%)

# Call groupedTable without a grouping variable: data %>% groupedTable(question_labels = labels)

Question

Response

n = 121

Gender

Female

7 (58%)

Male

5 (42%)

Ethnicity

Asian

3 (25%)

Black

2 (17%)

Hispanic or Latino

3 (25%)

white

4 (33%)

Year in school

grad

5 (42%)

undergrad

7 (58%)

Department of
Affiliation

Biology

5 (42%)

Chemistry

4 (33%)

Physics

3 (25%)

Cohort

1

6 (50%)

2

6 (50%)

1n (%)