Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to calculate the proportion of females in two different groups in the same dataset?

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 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading