【问题标题】:Summarise grouping by varname按 varname 汇总分组
【发布时间】:2019-03-08 19:52:01
【问题描述】:

我正在分析一些数据,但遇到了一个难题 - 我不知道如何使用变量名称作为“组”来总结我的整个数据集。

样本数据:

structure(list(x4 = c(3, 4, 7, 4, 5, 1, 5, 2, 7, 1), x5 = c(2, 
4, 4, 4, 5, 3, 6, 1, 7, 1), x6 = c(3, 5, 4, 7, 5, 4, 6, 4, 6, 
2), x7 = c(4, 1, 6, 4, 6, 4, 6, 2, 7, 2), x9 = c(5, 5, 4, 5, 
6, 3, 7, 5, 6, 1), x10 = c(3, 6, 5, 4, 6, 5, 6, 3, 6, 1), x11 = c(6, 
7, 7, 7, 6, 7, 7, 5, 7, 4), x12 = c(6, 7, 6, 7, 6, 4, 6, 6, 7, 
5), x14 = c(5, 7, 5, 6, 4, 6, 6, 5, 6, 4), x15 = c(4, 7, 7, 7, 
6, 4, 6, 5, 6, 1), x16 = c(4, 7, 7, 7, 6, 5, 7, 3, 6, 4), x17 = c(4, 
5, 5, 7, 6, 6, 7, 4, 6, 2), x18 = c(3, 4, 7, 7, 6, 5, 6, 4, 6, 
2), x19 = c(5, 7, 5, 7, 6, 6, 6, 3, 6, 1), x22 = c(4, 4, 5, 7, 
6, 7, 6, 5, 6, 2), x26 = c(6, 7, 5, 4, 6, 7, 7, 4, 6, 4), x29 = c(4, 
7, 2, 7, 6, 4, 7, 3, 6, 1), x33 = c(3, 7, 7, 7, 6, 5, 6, 3, 6, 
3), x34 = c(5, 5, 4, 7, 6, 7, 7, 5, 6, 2), x35 = c(4, 4, 7, 7, 
5, 7, 6, 4, 6, 2), x36 = c(4, 7, 6, 7, 6, 5, 5, 4, 6, 2), x37 = c(3, 
4, 7, 4, 5, 4, 6, 3, 5, 2), x49 = c(4, 7, 7, 7, 6, 5, 5, 6, 6, 
3), x50 = c(4, 7, 6, 5, 5, 5, 6, 5, 7, 4)), row.names = c(NA, 
-10L), class = "data.frame", .Names = c("x4", "x5", "x6", "x7", 
"x9", "x10", "x11", "x12", "x14", "x15", "x16", "x17", "x18", 
"x19", "x22", "x26", "x29", "x33", "x34", "x35", "x36", "x37", 
"x49", "x50"))

我只想要一些统计数据,像这样:

summary <- dados_afc %>% 
  summarise_all(funs(mean, sd, mode, median))

但结果是一个具有一个观察值和很多变量的 df。我希望它有 5 列:varname、mean、sd、mode、median,但我不知道该怎么做。有什么建议吗?

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    注意:我不知道从 R 获取模式的内置方法。请参阅此处进行一些讨论: Is there a built-in function for finding the mode?

    # From the top answer there:
    Mode <- function(x) {
      ux <- unique(x)
      ux[which.max(tabulate(match(x, ux)))]
    }
    

    要将每列视为一个组,您可以使用tidyr::gather 将“宽”数据转换为长格式,然后使用dplyr::group_by 创建具有自己的汇总计算的组:

    library(tidyverse)  
    summary <- dados_afc %>%
      gather(group, value) %>%
      group_by(group) %>%
      summarise_all(funs(mean, sd, Mode, median))
    
    
    >   summary
    # A tibble: 24 x 5
       group  mean    sd    Mode median
       <chr> <dbl> <dbl>   <dbl>  <dbl>
     1 x10     4.5 1.72        6    5  
     2 x11     6.3 1.06        7    7  
     3 x12     6   0.943       6    6  
     4 x14     5.4 0.966       6    5.5
     5 x15     5.3 1.89        7    6  
     6 x16     5.6 1.51        7    6  
     7 x17     5.2 1.55        6    5.5
     8 x18     5   1.70        6    5.5
     9 x19     5.2 1.87        6    6  
    10 x22     5.2 1.55        6    5.5
    

    【讨论】:

    • 完全忘记了收集... ty!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2015-09-02
    相关资源
    最近更新 更多