2017-05-29 50 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