I am a beginner coder so bear with me
I have a (non-shareable) data frame of cases of disease (each row is a person with a disease) and columns corresponding to their age group (Age_Grp_15) and their quintile of marginalization (AGE_LABOURFORCE_Q_CSDUID).
I want to create a summary table of case counts based on age and marginalization. So far, I have:
count_age <- marg_data_1920 %>%
group_by(Age_Grp_15, AGE_LABOURFORCE_Q_CSDUID) %>%
summarise(count = n()) %>%
spread(AGE_LABOURFORCE_Q_CSDUID, count, fill = 0)
Here is something I have done successfully with the same code. All cells fill with numbers as they should
https://i.sstatic.net/gwa4pLrI.png
However, my disease data frame does not have any cases in my first quintile (see: https://i.sstatic.net/oTolfWPA.png). I want to force R to list the first quintile column and fill it with zeroes in cases when there is no data. I would appreciate my code forcing any empty column to show up
I have tried the code below suggested by ChatGPT, which has not worked at all
count_age <- marg_data_1920 %>%
group_by(Age_Grp_15, AGE_LABOURFORCE_Q_CSDUID) %>%
summarise(count = n()) %>%
complete(Age_Grp_15, nesting(AGE_LABOURFORCE_Q_CSDUID), fill = list(count = 0)) %>%
spread(AGE_LABOURFORCE_Q_CSDUID, count, fill = 0) %>%
rename(age = Age_Grp_15)
colnames(count_age) <- c("age", "q_1","q_2","q_3","q_4","q_5")
user25561433 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.