Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A more elegant way to compute within-group proportions in dplyr? [duplicate]

Tags:

r

dplyr

tidyverse

Given a data_frame df <- data_frame(X = c('A', 'A', 'B', 'B', 'B'), Y = c('M', 'N', 'M', 'M', 'N')), I need to come up with a data_frame that tells us that 50% of A's are M, 50% of A's are N, 67% of B's are M, and 33% of B's are N.

I have a little routine that I use to do it, but it seems horrible.

library(tidyverse)
df <- data_frame(X = c('A', 'A', 'B', 'B', 'B'), Y = c('M', 'N', 'M', 'M', 'N')) 
# here we go...
df %>% 
  group_by(X) %>% 
  mutate(n_X = n()) %>% 
  group_by(X, Y) %>% 
  summarise(PERCENT = n() / first(n_X))

which outputs,

Source: local data frame [4 x 3]
Groups: X [?]

      X     Y   PERCENT
  <chr> <chr>     <dbl>
1     A     M 0.5000000
2     A     N 0.5000000
3     B     M 0.6666667
4     B     N 0.3333333

Is there not a better way to do this? Surely I'm missing something.

like image 330
crf Avatar asked Jan 24 '17 06:01

crf


1 Answers

You can use prop.table:

df %>% 
  group_by(X, Y) %>%
  count() %>%
  mutate(PERCENT = prop.table(n))

The result:

      X     Y     n   PERCENT
  <chr> <chr> <int>     <dbl>
1     A     M     1 0.5000000
2     A     N     1 0.5000000
3     B     M     2 0.6666667
4     B     N     1 0.3333333
like image 92
Sven Hohenstein Avatar answered Oct 01 '22 22:10

Sven Hohenstein