If I have a simple data frame with 2 factors (a and b) with 2 levels (1 and 2) and 1 variable (x), how do I get the median values of x: median x over each level of factor a, each level of factor b, and each combination of a*b?
library(dplyr)
df <- data.frame(a = as.factor(c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2)),
b = as.factor(c(1,1,1,1,2,2,2,2,1,1,1,1,2,2,2,2)),
x = c(runif(16)))
I've tried various (many) versions of:
df %>%
group_by_(c("a", "b")) %>%
summarize(med_rate = median(df$x))
The results should look like this for the median x of each level of factor a:
a median
1 0.58811
2 0.53167
And like this for the median x of each level of factor b:
b median
1 0.60622
2 0.46096
And like this for the median x for each combinations of a and b:
a b median
1 1 0.66745
1 2 0.34656
2 1 0.50903
2 2 0.55990
Thanks in advance for any help.