【问题标题】:Summary statistics in r, measures as headersr 中的汇总统计信息,作为标题的度量
【发布时间】:2021-03-29 19:08:38
【问题描述】:

我想为我的数据集创建汇总统计信息。我试过搜索,但没有找到任何符合我想要的东西。我希望以统计度量作为标题垂直列出列。这是我想要的样子:

Column Mean Standard deviation 25th perc. Median 75th perc.
Column 1 Mean column 1 Std column 1 ... ... ...
Column 2 Mean column 2 ... ... ... ...
Etc ... ... ... ... ...

我该怎么做?感谢我能得到的任何帮助!:) 如果有一个特定的功能可以使用,我也可以做一些格式化/样式化,一些关于它的信息也将不胜感激,但重点是它应该看起来像描述的那样。 :)

【问题讨论】:

  • 如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则更容易为您提供帮助。您是否尝试过编写任何代码来做到这一点?你到底在哪里卡住了?

标签: r statistics


【解决方案1】:

您可能想查看summarytools 包...内置了对 markdown 和 html 的支持。

library(summarytools)  
descr(iris, 
      stats = c("mean", "sd", "q1", "med", "q3"),
      transpose = TRUE)

## Non-numerical variable(s) ignored: Species
## Descriptive Statistics  
## iris  
## N: 150  
##
##                     Mean   Std.Dev     Q1   Median     Q3
## ----------------- ------ --------- ------ -------- ------
##      Petal.Length   3.76      1.77   1.60     4.35   5.10
##       Petal.Width   1.20      0.76   0.30     1.30   1.80
##      Sepal.Length   5.84      0.83   5.10     5.80   6.40
##       Sepal.Width   3.06      0.44   2.80     3.00   3.30

【讨论】:

    【解决方案2】:

    我们可以使用来自collapsedescr

    library(collapse)
    descr(iris)
    

    【讨论】:

      【解决方案3】:

      你的问题缺少一些重要的功能,但我认为你想要这样的东西:

      仅包含 iris 数据集的数值变量的示例:

      iris_numerical<-iris[,1:4]
      

      计算统计数据

      new_df<-sapply(iris_numerical, function(x){c(mean=mean(x), SD=sd(x), Q1=quantile(x, 0.25), median=median(x), Q3=quantile(x, 0.75))})
      

      这为您提供按列的汇总统计信息

      > new_df
             Sepal.Length Sepal.Width Petal.Length Petal.Width
      mean      5.8433333   3.0573333     3.758000   1.1993333
      SD        0.8280661   0.4358663     1.765298   0.7622377
      Q1.25%    5.1000000   2.8000000     1.600000   0.3000000
      median    5.8000000   3.0000000     4.350000   1.3000000
      Q3.75%    6.4000000   3.3000000     5.100000   1.8000000
      

      然后以所需格式创建最终数据框,将列名作为行名:

      new_df<-data.frame(column=colnames(new_df), apply(new_df, 1, function(x) x))
      > new_df
                         column     mean        SD Q1.25. median Q3.75.
      Sepal.Length Sepal.Length 5.843333 0.8280661    5.1   5.80    6.4
      Sepal.Width   Sepal.Width 3.057333 0.4358663    2.8   3.00    3.3
      Petal.Length Petal.Length 3.758000 1.7652982    1.6   4.35    5.1
      Petal.Width   Petal.Width 1.199333 0.7622377    0.3   1.30    1.8
      

      【讨论】:

      • 是的,这正是我想要的。谢谢! :D
      • 我还建议查看 summarytools 包,正如@Dominic Comtois 所建议的那样
      猜你喜欢
      • 2015-09-22
      • 1970-01-01
      • 2021-08-12
      • 2018-08-11
      • 2012-01-07
      • 2016-01-28
      • 2019-03-19
      • 2021-08-03
      • 2020-12-03
      相关资源
      最近更新 更多