katdataecon
katdataecon

Reputation: 185

Proportions of character variables based on multiple variables and subgroups in R

I have two columns in a dataset that represent meetings

Occupation Gender
Director M
Director F
VP M
Director F
Secretary F
Director F

So here we have 6 meetings, and these are the two proportions i'd like to obtain :

1 - Prop. of female director meetings / all the meetings (3/6) 2 - Prop. of female director meetings / meetings attended by women (3/4)

Of course i have thousands of observations.

I tried with the solution given here How to calculate proportions from multiple variables but i don't obtain the results that i want.

I'm pretty sure that is easy but i don't know how to take into account the "if" conditions. Also, i'd like to present the results through chart so it's better if the output is a data frame.

Upvotes: 0

Views: 641

Answers (1)

IRTFM
IRTFM

Reputation: 263421

prop.table takes a table object as input. This is what you get from a call that doesn't specify a margin (which I think answers your first question):

prop.table(table(dat[[1]],dat[[2]])) 
# could also be done on `table( dat$Occupation, dat$Gender)`
              
                 F         M     
   Director   0.5000000 0.1666667
   Secretary  0.1666667 0.0000000
   VP         0.0000000 0.1666667

And you probably also want row proportions, easily obtained by using MARGIN of 1 (the R convention for rows).

prop.table(table(dat[[1]],dat[[2]]), 1)
             
               F       M     
   Director      0.75    0.25
   Secretary     1.00    0.00
   VP            0.00    1.00

#-----data construction -----

> TXT <- "
+ | Occupation| Gender|
+ | Director  | M     |
+ | Director  | F     |
+ | VP        | M     |
+ | Director  | F     |
+ | Secretary | F     |
+ | Director  | F     |"
> dat <- read.table(text=TXT, head=T,sep='|')[-c(1,4)]
> dat
   Occupation  Gender
1  Director    M     
2  Director    F     
3  VP          M     
4  Director    F     
5  Secretary   F     
6  Director    F     

Upvotes: 1

Related Questions