【问题标题】:Creating a three-way table of summary statistics in R在 R 中创建汇总统计信息的三向表
【发布时间】:2014-02-01 15:01:51
【问题描述】:

示例数据

我有 100 行患者数据存储在对象 example 中。对于每位患者,我们知道他们在五家可能的医院中的哪一家接受治疗、他们接受治疗的时间段以及他们有多少淋巴结。

set.seed(50)

example <- data.frame(
Hospital = sample(as.factor(c("Hospital 1", "Hospital 2", "Hospital 3", "Hospital 4", "Hospital 5")), size = 100, replace = TRUE),
Time = sample(as.factor(c("2000-2002", "2003-2005", "2006-2008")), size = 100, replace = TRUE),
Nodes = sample(20:100, size = 100, replace = TRUE))

我知道我可以像这样查看淋巴结数量的汇总统计信息...(请注意,我已将“n”附加到最右侧的列中,不确定是否有更雄辩的方法这样做。)

cbind(do.call(rbind, by(example$Nodes, example$Hospital, summary)), table(example$Hospital, useNA = "no"))

             Min. 1st Qu. Median  Mean 3rd Qu. Max.   
  Hospital 1   20   34.25   54.0 55.55   77.75   90 22
  Hospital 2   22   38.75   60.5 56.25   71.75   94 20
  Hospital 3   22   37.00   51.0 57.12   81.00   96 17
  Hospital 4   25   39.75   55.5 57.11   72.25   97 28
  Hospital 5   26   42.00   50.0 57.00   77.00   99 13

同样,我可以像这样查看它们的时间段:

cbind(do.call(rbind, by(example$Nodes, example$Time, summary)), table(example$Time, useNA = "no"))
            Min. 1st Qu. Median  Mean 3rd Qu. Max.   
  2000-2002   20   40.00   57.0 58.84      77   97 37
  2003-2005   20   33.75   45.5 52.94      78   99 36
  2006-2008   23   39.50   61.0 58.33      72   98 27

问题

我想创建一个三向表,其中最左侧的最外行标识符是五家医院,并按时间段进一步细分。我希望这些列是淋巴结数量的汇总统计数据。我觉得 xtabs() 或 ftable() 可能会有所帮助,但不知道如何将它们应用于我的问题。事实上,输入ftable(example) 给了我一个表,它的结构是我想要的,但列不是我想要的。谢谢!

编辑 #1 - 回应 Ananda 下面的评论

哇,是的,这几乎正是我想要的。然而,我的偏好是采用这种格式(当然要填写数字):

                     Nodes
                     Min.  1st Qu.  Median  Mean 3rd Qu.  Max.  n
Hospital   Time 
Hospital 1 2000-2002 
           2003-2005
           2006-2008
Hospital 2 2000-2002  
           2003-2005
           2006-2008

....and so forth....

【问题讨论】:

  • 你的意思是aggregate(Nodes ~ Hospital + Time, example, summary)
  • 如果你想引起别人的注意,你需要在他们的用户名前使用@ ping 他们。我不认为你正在寻找的东西在 R 中很容易获得。ftable 是我所知道的 R 中唯一提供这种嵌套行和列属性的函数,实际数据在矩阵中,所以如果你真的想要那个嵌套的演示文稿,你可能需要拼凑你自己的自定义“as.ftable”之类的东西。

标签: r


【解决方案1】:

对上面提到的@AnandaMahto 的aggregate() 函数产生的数据帧进行排序将提供非常接近您需要的东西,但没有嵌套值:

    dF <- aggregate(Nodes~Hospital+Time, example, summary)
    dF <- dF[order(dF[, 1]), ]

         Hospital      Time Nodes.Min. Nodes.1st Qu. Nodes.Median Nodes.Mean Nodes.3rd Qu.
    1  Hospital 1 2000-2002      20.00         25.00        34.00      33.29         38.00
    6  Hospital 1 2003-2005      20.00         41.50        77.00      62.86         85.50
    11 Hospital 1 2006-2008      35.00         60.50        70.50      68.62         80.75
    2  Hospital 2 2000-2002      24.00         40.75        65.50      60.70         80.75
    7  Hospital 2 2003-2005      22.00         22.00        26.00      33.75         37.75
    12 Hospital 2 2006-2008      45.00         60.25        61.50      63.83         68.00
    3  Hospital 3 2000-2002      40.00         63.00        74.00      72.80         91.00
    8  Hospital 3 2003-2005      22.00         36.75        66.00      60.50         81.75
    13 Hospital 3 2006-2008      23.00         29.50        37.00      40.67         46.75
    4  Hospital 4 2000-2002      30.00         55.75        64.50      68.17         90.00
    9  Hospital 4 2003-2005      25.00         38.25        42.00      49.36         59.50
    14 Hospital 4 2006-2008      27.00         36.00        45.00      45.00         54.00
    5  Hospital 5 2000-2002      26.00         39.00        52.00      51.67         64.50
    10 Hospital 5 2003-2005      34.00         42.00        50.00      55.40         52.00
    15 Hospital 5 2006-2008      30.00         42.00        48.00      61.80         91.00
    Nodes.Max.
    1       53.00
    6       89.00
    11      90.00
    2       94.00
    7       61.00
    12      85.00
    3       96.00
    8       95.00
    13      70.00
    4       97.00
    9       89.00
    14      63.00
    5       77.00
    10      99.00
    15      98.00      

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-03
    • 2021-08-03
    • 2015-09-22
    • 1970-01-01
    • 2021-08-12
    • 2018-08-11
    • 1970-01-01
    • 2012-01-07
    相关资源
    最近更新 更多