If I wanted to apply multiple functions in a dplyr call, I could run the following:
mtcars %>% group_by(gear) %>% select(hp, disp) %>%
summarise_all(funs(n=sum(!is.na(.)), mean=mean(.,na.rm=T)))
Output:
# A tibble: 3 x 5
gear hp_n disp_n hp_mean disp_mean
<dbl> <int> <int> <dbl> <dbl>
1 3 15 15 176.1333 326.3000
2 4 12 12 89.5000 123.0167
3 5 5 5 195.6000 202.4800
This is great and exactly what I want. Now if I wanted to melt this to make it into a pivot table (we use a lot of pivot tables at my job), I could just call melt
. However the result would make both the _mean
and the _n
columns into their own rows. So if I run the following:
mtcars %>% group_by(gear) %>% select(hp, disp) %>%
summarise_all(funs(n=sum(!is.na(.)), mean=mean(.,na.rm=T))) %>%
melt(id.vars="gear")
I will get
gear variable value
1 3 hp_n 15.0000
2 4 hp_n 12.0000
3 5 hp_n 5.0000
4 3 disp_n 15.0000
5 4 disp_n 12.0000
6 5 disp_n 5.0000
7 3 hp_mean 176.1333
8 4 hp_mean 89.5000
9 5 hp_mean 195.6000
10 3 disp_mean 326.3000
11 4 disp_mean 123.0167
12 5 disp_mean 202.4800
When what I really want is:
gear variable metric value
1 3 hp n 15.0000
2 3 hp mean 176.1333
3 5 disp n 15.0000
4 5 disp mean 326.3000
....
How do I go about doing this with dplyr
? Do I need to re-work the melt
command?