Given a data_frame df <- data_frame(X = c('A', 'A', 'B', 'B', 'B'), Y = c('M', 'N', 'M', 'M', 'N')), I need to create a data_frame that tells us that 50% Aare M, 50% A- N, 67% of B M, and 33% B- N.
I have a little routine that I use for this, but it seems awful.
library(tidyverse)
df <- data_frame(X = c('A', 'A', 'B', 'B', 'B'), Y = c('M', 'N', 'M', 'M', 'N'))
df %>%
group_by(X) %>%
mutate(n_X = n()) %>%
group_by(X, Y) %>%
summarise(PERCENT = n() / first(n_X))
which issues
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 a better way to do this? Of course, I missed something.
source
share