2017-05-29 45 views
1

我想創建一個分組摘要,報告每個組中的記錄數,然後顯示一系列變量的含義。如何在一個命令中組合兩個不同的dplyr摘要

我只能研究如何做到這兩個單獨的總結,然後我將它們連接在一起。這工作正常,但我想知道是否有一個更優雅的方式來做到這一點?

dailyn<-daily %>% # this summarises n 
    group_by(type) %>% 
    summarise(n=n()) %>% 

dailymeans <- daily %>% # this summarises the means 
    group_by(type) %>% 
    summarise_at(vars(starts_with("d.")),funs(mean(., na.rm = TRUE))) %>% 

dailysummary<-inner_join(dailyn,dailymeans) #this joins the two parts together 

我正在使用的數據是這樣的數據幀:

daily<-data.frame(type=c("A","A","B","C","C","C"), 
        d.happy=c(1,5,3,7,2,4), 
        d.sad=c(5,3,6,3,1,2)) 
+0

你能分享你的數據樣本嗎? – Sotos

回答

3

您可以在一次通話中做到這一點,通過分組,利用變異,而不是總結,然後用切片(),以確保每種類型的第一行:

daily %>% group_by(type) %>% 
    mutate(n = n()) %>% 
    mutate_at(vars(starts_with("d.")),funs(mean(., na.rm = TRUE))) %>% 
    slice(1L) 

編輯:這可能是更清晰的是如何工作的,在這個變型實例

daily_summary <- daily %>% group_by(type) %>% 
    mutate(n = n()) %>% 
    mutate_at(vars(starts_with("d.")),funs("mean" = mean(., na.rm = TRUE))) 

daily_summary 
# Source: local data frame [6 x 6] 
# Groups: type [3] 
# 
# # A tibble: 6 x 6 
# type d.happy d.sad  n d.happy_mean d.sad_mean 
# <fctr> <dbl> <dbl> <int>  <dbl>  <dbl> 
#1  A  1  5  2  3.000000   4 
#2  A  5  3  2  3.000000   4 
#3  B  3  6  1  3.000000   6 
#4  C  7  3  3  4.333333   2 
#5  C  2  1  3  4.333333   2 
#6  C  4  2  3  4.333333   2 

daily_summary %>% 
    slice(1L) 

# Source: local data frame [3 x 6] 
# Groups: type [3] 
# 
# # A tibble: 3 x 6 
# type d.happy d.sad  n d.happy_mean d.sad_mean 
# <fctr> <dbl> <dbl> <int>  <dbl>  <dbl> 
#1  A  1  5  2  3.000000   4 
#2  B  3  6  1  3.000000   6 
#3  C  7  3  3  4.333333   2 
+0

這工作很好,但我不明白它在做什麼。當你說'保持每種類型的第一行'時,那是什麼?爲什麼第一行包含手段? – mob

+1

使用mutate而不是彙總確保我們保留所有數據,並且可以在同一個數據框中執行計算和平均值計算。如果您在使用slice()函數之前查看結果,則會看到您擁有三種類型的分組數據框,並且每個觀測值仍有一行。 'slice(1L)'然後保持每種類型的第一行(其中type是我們分組的變量)。 – emiltb

+0

改寫:當使用mutate_at所有行最終包含計數和平均值時(請參閱我的更新示例),因此保留第一行並不重要。它可以是任何行。 – emiltb

1

類似this question,你可以嘗試:

daily %>% 
    group_by(type) %>% 
    mutate(n = n()) %>% 
    mutate_at(vars(starts_with("d.")),funs(mean(., na.rm = TRUE))) %>% 
    unique 

這給:

Source: local data frame [3 x 4] 
Groups: type [3] 

    type d.happy d.sad  n 
    <fctr> <dbl> <dbl> <int> 
1  A 3.000000  4  2 
2  B 3.000000  6  1 
3  C 4.333333  2  3 
+0

這適用於我給出的示例數據,但我的實際數據集有一大堆額外的列。由於某些原因,當出現額外的列時,分組不起作用。 – mob

+1

那麼,我們只能使用示例數據,對不起。請閱讀[這裏](https://stackoverflow.com/help/mcve) – Aramis7d