简体   繁体   中英

dplyr group by multiple variables summarise by multiple variables

New to R. Using dplyr, trying to group_by multiple variables, summarize by multiple variables, multiple functions.

This works as expected

mtcars %>% 
+     group_by(cyl,hp) %>% 
+     summarise(min_mpg = min(mpg) , min_disp = min(disp), max_mpg = max(mpg) , max_disp = max(disp))

But when I try to replicate with my df

vmp %>% 
    +     group_by(Priority,LOS) %>% 
    +     summarise(inv_total = sum(Inv_Total), sr_count = count(SR_Nmbr))

I receive this error:

Error in summarise_impl(.data, dots) : Evaluation error: no applicable method for 'groups' applied to an object of class "factor".

What am I doing wrong? Thx

library(dplyr)
vmp %>%
     mutate(Inv_Total=as.numeric(as.character(Inv_Total))) %>% 
     group_by(Priority,LOS) %>%
     summarise(sr_count=n(), 
               inv_total=sum(Inv_Total))

We can use type.convert to convert the column types automatically

vmp %>%
    mutate_all(funs(type.convert(as.character(.), as.is = TRUE))) %>%
    group_by(Priority, LOS) %>%
    summarise(inv_total = sum(Inv_Total), sr_count =n())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM