'Calculate quantiles for each combination of two categorical variables in data frame
I would like to calculate quantiles for each combination of two categorical variables. E.g.
df <- data.frame(x=1:100, y=c(rep("A", 50), rep("B", 50)), y2=c(rep("C", 25), rep("D", 25),rep("E", 25), rep("F", 25)))
I tried solution shown in Use ntile() with group_by() with dplyr by extending to group_by(y,y2) %>% mutate(z=ntile(x,2)) but did not get the expected result of two quantile groups per combination of y and y2.
df1 <- df %>%
group_by(y) %>% mutate(z = ntile(x, 2))
df2 <- df %>%
group_by(y,y2) %>% mutate(z = ntile(x, 2))
df1 == df2 #suggests they are identical
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
