In my data, I have a list of respondents who listed their age and gender. I have assigned a value of 1 of those who are 50+ in age. How would I calculate the proportion of females in the data in two different groups. respondents over 50 and respondents under 50?
My data (when selecting only relevant variables) looks like:
Gender Age fiftyplus
M 23 0
F 43 0
M 53 1
M 50 1
F 84 1
M 45 0
F 79 1
>Solution :
Here is one possible way to do it:
library(dplyr)
df %>%
count(Gender, fiftyplus) %>%
group_by(Gender) %>%
mutate(prop = prop.table(n))
Gender fiftyplus n prop
<chr> <int> <int> <dbl>
1 F 0 1 0.333
2 F 1 2 0.667
3 M 0 2 0.5
4 M 1 2 0.5
data:
df <- structure(list(Gender = c("M", "F", "M", "M", "F", "M", "F"),
Age = c(23L, 43L, 53L, 50L, 84L, 45L, 79L), fiftyplus = c(0L,
0L, 1L, 1L, 1L, 0L, 1L)), class = "data.frame", row.names = c(NA,
-7L))