2017-08-11 88 views
0

R的新成員 使用dplyr,嘗試group_by多個變量,彙總多個變量,多個函數。dplyr組由多個變量彙總多個變量

可正常工作

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

但是,當我試圖用我的DF

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

複製我收到此錯誤:

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

我在做什麼錯? THX

回答

0
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)) 
0

我們可以使用type.convert自動轉換列類型

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