I am trying to calculate the percentages for cigarettes smoking status by sex (for example, the % of males/females who are Non-smokers, Occasional smokers, Prefer not to say, Regular smokers etc). The default seems to calculate the percentage from the Row Total and not the Column Total. Any help would be greatly appreciated.
Dataframe
structure(list(sex = c("Female", "Male", "Female", "Female"),
cigarettes_smoking_status = c("Non-smoker", "Non-smoker",
"Non-smoker", "Non-smoker")), row.names = c(NA, 4L), class = "data.frame")
Code
smoking_status_by_sex <- smoking_data %>%
group_by(sex) %>%
dplyr::count(cigarettes_smoking_status) %>%
pivot_wider(names_from = sex, values_from = n) %>% #increase number of columns & reduce rows
adorn_totals(c("row", "col") )
smoking_status_by_sex_per <- smoking_status_by_sex %>%
mutate(female_pct = round((100*.[[2]]/Total),digits =2),
male_pct = round((100*.[[3]]/Total),digits =2),
prefer_not_to_say_pct = round((100*.[[4]]/Total), digits=2),
unknown_pct = round((100*.[[5]]/Total),digits =2),
total_pct = round((100*.[[6]]/Total), digits=2))
This is the table I am trying to replicate below [What I am trying to replicate][1] [1]: https://i.stack.imgur.com/hhDA4.png
I have tried using count, colSum, adorn_totals etc and then tried to use pivot_wider. Any help would be greatly appreciated.