2

I am working on a dataset using dplyr library. I am struggling by trying to group some variables and calculating the mean and sum in one command line using "summarise_at" function.

Using the following code, I got an error.

complete.data %>%
  select(A, B, C, D, E, F) %>%
  group_by(A) %>%
  summarise_at(vars(B, C, D), mean) %>%
  summarise_at(vars(E, F, G), sum)

Moreover, I reckon that I would get a table with the following order of variables as output:

group_by (A), mean variables (B,C,D), sum variables (E,F,G) 

with the related data under each column.

I would like to obtain the variables following sequence:

A(group_by)     D        B       E      C         F 

Could you please suggest me a way to obtain the desired result?

Shree
  • 10,835
  • 1
  • 14
  • 36
DaniB
  • 200
  • 2
  • 15

1 Answers1

0

Here's a way that is not covered in my linked duplicate in comments. This is a horrible way but could be okay if data is small and/or your summary functions are fast. Basically I am applying all functions to all desired variables and then selecting the needed combinations.

Personally, I don't think this worth it but just putting it out here -

mtcars %>% 
  group_by(cyl) %>% 
  summarize_at(c(3,4,5,6), list(mean = ~mean(.), sum = ~sum(.), median = ~median(.))) %>% 
  select(cyl, hp_mean, drat_sum, wt_median, qsec_median) 
  # select can be automated based on patterns

# A tibble: 3 x 5
    cyl hp_mean drat_sum wt_median qsec_median
  <dbl>   <dbl>    <dbl>     <dbl>       <dbl>
1     4    82.6     44.8      2.2         18.9
2     6   122.      25.1      3.22        18.3
3     8   209.      45.2      3.76        17.2
Shree
  • 10,835
  • 1
  • 14
  • 36