I am calculating median and values for different quantiles for a continuous variable. I want to add all the columns in a single step. Is this possible to do this. Following is a reproducible example.
df <- data.frame(group = rep(c('group1','group2'),50),
x = rnorm(100),
y = rnorm(100))
df %>%
gather('variable','value', -group) %>%
group_by(group, variable) %>%
summarise(median = round(quantile(value,0.5, na.rm = T),2),
iqr25 = round(quantile(value,0.25, na.rm = T),2),
iqr75 = round(quantile(value,0.75, na.rm = T),2))
OUTPUT
# A tibble: 4 x 5
# Groups: group [2]
group variable median iqr25 iqr75
<fct> <chr> <dbl> <dbl> <dbl>
1 group1 x 0.06 -0.74 1.04
2 group1 y -0.36 -1.03 0.45
3 group2 x -0.04 -0.85 0.62
4 group2 y 0.06 -0.56 0.89
Can this summarise step be done without writing the quantile function 3 times.
I did a work around using this. But is there a nice way to do this.
df %>%
gather('variable','value', -group) %>%
group_by(group, variable) %>%
summarise(s = toString(round(quantile(value, c(0.25,0.5,0.75),na.rm = T),2))) %>%
separate(s, into = c('q25','median','q75'), sep = ',')