【问题标题】:How to generate five number summary using describe.by如何使用 describe.by 生成五个数字摘要
【发布时间】:2013-12-10 15:23:34
【问题描述】:

我想知道如何从summary() 提供的describeBy() 函数中调用五位数的统计信息。调用library(psych) 后,当前函数报告最小值、最大值和中位数,但不报告第 25 和第 75 个四分位数的统计信息。

attach(mtcars)
describeBy(mpg, gear)

var  n  mean   sd median trimmed  mad  min  max range  skew kurtosis   se 

提前感谢您的帮助。

【问题讨论】:

    标签: r aggregate summary describe describe.by


    【解决方案1】:

    还有一个专门针对五个数字的内置基函数,不出所料地称为:fivenum

    aggregate(mpg ~ gear, data=mtcars, fivenum)
      gear mpg.1 mpg.2 mpg.3 mpg.4 mpg.5
    1    3 10.40 14.50 15.50 18.40 21.50
    2    4 17.80 21.00 22.80 28.85 33.90
    3    5 15.00 15.80 19.70 26.00 30.40
    

    编辑 要回答 cmets 中的后续 Q(正如我所解释的那样),您可以在公式中使用 . 来指定所有其他列

    aggregate(.~gear, data=mtcars, fivenum)
    #too wide to print here
    

    或者,如果您只希望所有列的 fivenum 没有齿轮拆分,那么就是:

        apply(mtcars, 2,fivenum)
           mpg cyl   disp  hp  drat     wt   qsec vs am gear carb
    [1,] 10.40   4  71.10  52 2.760 1.5130 14.500  0  0    3    1
    [2,] 15.35   4 120.65  96 3.080 2.5425 16.885  0  0    3    2
    [3,] 19.20   6 196.30 123 3.695 3.3250 17.710  0  0    4    2
    [4,] 22.80   8 334.00 180 3.920 3.6500 18.900  1  1    4    4
    [5,] 33.90   8 472.00 335 4.930 5.4240 22.900  1  1    5    8
    

    【讨论】:

    • 谢谢斯蒂芬。有用!你知道是否可以同时为整体调用 Fivenum 吗?
    • @Lily 我试图回答我认为你在问的问题。如果是这样,请勾选关闭。
    • 感谢您的回复。我实际上是要求在等于结果分位数(mpg)的聚合(mpg ~ gear,data=mtcars,fivenum)结果中添加第四行。
    【解决方案2】:

    更简单:您只需添加 describeBy 参数 quant=c(0.25,0.75))

    【讨论】:

      猜你喜欢
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 2019-05-05
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      相关资源
      最近更新 更多