I have some data where I am hoping to calculate the proportion who experienced each of the h3ma variables, both overall and broken down by the respective “subgroups” within the category column.
My end goal is to produce a figure like the one below. I’ve been previously just calculating the proportions manually one by one and then creating a new dataframe but there must be a more efficient way.
Data:
d <- structure(list(h3ma1_binary = c(1, 0, 1, 1, 0, 0, 0, 0, 1, NA,
0, 1, NA, 1, 1), h3ma2_binary = c(0, 0, 0, 1, NA, 0, 1, NA, 0,
1, NA, 0, 1, 1, 1), h3ma3_binary = c(1, 1, 0, 0, 0, 0, 1, 1,
NA, 0, 1, NA, 1, 0, 0), h3ma4_binary = c(1, 0, 0, 0, 0, 0, 0,
0, NA, 1, 1, NA, 0, 0, 1), race = c("White", "White", "Black",
"Black", "White", "Hispanic", "White", "White", "Hispanic", "White",
"Black", "White", "White", "Black", "White"), parent_welfare = c(0,
0, 0, 1, 0, 1, 0, NA, 1, 1, 0, 1, NA, 1, 1), parent_dropout...7 = c(1,
0, 1, 0, NA, 1, 1, 0, 1, 0, NA, 1, 1, 0, 1), parent_dropout...8 = c(1,
0, 1, 0, NA, 1, 1, 0, 1, 0, NA, 1, 1, 0, 1)), row.names = c(NA,
-15L), spec = structure(list(cols = list(h3ma1_binary = structure(list(), class = c("collector_double",
"collector")), h3ma2_binary = structure(list(), class = c("collector_double",
"collector")), h3ma3_binary = structure(list(), class = c("collector_double",
"collector")), h3ma4_binary = structure(list(), class = c("collector_double",
"collector")), race = structure(list(), class = c("collector_character",
"collector")), parent_welfare = structure(list(), class = c("collector_double",
"collector")), parent_dropout...7 = structure(list(), class = c("collector_double",
"collector")), parent_dropout...8 = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x000001b255a94190>, class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))