0

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.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
katdataecon
  • 185
  • 8

1 Answers1

1

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     
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • `table` takes a data frame, so here `table(dat)` would do. And I think `proportions` is the current name of `prop.table` ("an earlier name, retained for back-compatibility"). Cheers – Henrik Jul 05 '21 at 20:40
  • @Henrik: Maybe if you have a package or a current version of R but I'm on R 3.6 and I get `?proportions No documentation for ‘proportions’ in specified packages and libraries: you could try ‘??proportions’` – IRTFM Jul 05 '21 at 20:43
  • OK. `proportions` was introduced in `R 4.0.0`. "New function `proportions()` and `marginSums()`. These should replace the unfortunately named `prop.table()` and `margin.table()`. They are drop-in replacements, but also add named-margin functionality. The old function names are retained as aliases for back-compatibility." – Henrik Jul 05 '21 at 20:45
  • I updated to R 4.1.0 and now see this revision. And now I can play with `|>` `` – IRTFM Jul 05 '21 at 21:43