【问题标题】:Spread one of multiple grouping variables without applying function在不应用函数的情况下传播多个分组变量之一
【发布时间】:2015-08-21 07:55:10
【问题描述】:

无法让 tidyr、reshape 或 reshape2 完成我认为简单的事情。

拥有如下所示的数据:

    agecat  year    Drug                    total
    <1      1999    Amikacin                12
    <1      1999    Cefepime                3
    <1      1999    Ceftazidime             13
    <1      2000    Amikacin                3
    <1      2000    Cefepime                6
    <1      2000    Ceftazidime             3
    <1      2000    Ciprofloxacin           4
    1-5     1999    Amikacin                37
    1-5     1999    Cefepime                25
    1-5     1999    Ceftazidime             38
    1-5     1999    Ciprofloxacin           38
    1-5     2000    Amikacin                52
    1-5     2000    Cefepime                34
    1-5     2000    Ceftazidime             45

..但更长的时间,有 4 个年龄猫,14 年(1999-2012 年)和 10 种药物,每种都有唯一的总数。我想传播“年份”并保持总数,基本上是为了按药物对agecat的趋势有一个广泛的“时间表”。即

agecat      drug       1999   2000   
<1          Amikacin    23      3    
<1          Cefepime     3      6

当只有两个分组变量时,我在 tidyr 中的传播非常幸运,但是添加第三个似乎会全部结束...我得到一个稀疏矩阵或名称匹配错误。使用melt/dcast,我尝试添加一个ID列(1:n行)但仍然没有成功......当我不想更改数据时,我无法理解应用什么功能,只需制作它宽幅。我错过了什么?谢谢!

【问题讨论】:

  • 您在寻找reshape2::dcast(df, agecat + Drug ~ year, value.var = "total")吗?
  • 嗨@user2706569,谢谢,我是这么认为的,但由于某种原因,“value.var”参数不起作用,它显示“缺少聚合函数:默认为长度”,这意味着每个条目都是1 或 2。
  • 在您显示的数据中,每个agecat 每年和药物只有一个观察值。在您的数据集中,情况似乎并非如此。因此,reshape2 会询问您如何汇总 agecat-drug-year 观察值。
  • 每个梳子只有一个观察值。 agecat + 年份 + 药物。我认为问题在于总数有时会重复,但放置一个唯一 ID(1:n 行)并运行 &gt; agem&lt;-dcast(age, agecat + Drug + ID ~ year, value.var = 'total') 会导致矩阵稀疏。
  • 更新:将原来的表格改成按 'year' 分组,然后按 'agecat' 允许第一个 reshape 函数 reshape2::dcast(df, agecat + Drug ~ year, value.var = "total") 工作!为什么?!

标签: r reshape2 tidyr


【解决方案1】:

您可以通过spread of tidyr 实现您的目标:

library(tidyr)
spread(df, year, total)

  agecat          Drug 1999 2000
1     <1      Amikacin   12    3
2     <1      Cefepime    3    6
3     <1   Ceftazidime   13    3
4     <1 Ciprofloxacin   NA    4
5    1-5      Amikacin   37   52
6    1-5      Cefepime   25   34
7    1-5   Ceftazidime   38   45
8    1-5 Ciprofloxacin   38   NA

【讨论】:

    【解决方案2】:

    来自问题的数据:

    df <- structure(list(agecat = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("<1", "1-5"), class = "factor"), 
        year = c(1999L, 1999L, 1999L, 2000L, 2000L, 2000L, 2000L, 
        1999L, 1999L, 1999L, 1999L, 2000L, 2000L, 2000L), Drug = structure(c(1L, 
        2L, 3L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L), .Label = c("Amikacin", 
        "Cefepime", "Ceftazidime", "Ciprofloxacin"), class = "factor"), 
        total = c(12L, 3L, 13L, 3L, 6L, 3L, 4L, 37L, 25L, 38L, 38L, 
        52L, 34L, 45L)), .Names = c("agecat", "year", "Drug", "total"
    ), class = "data.frame", row.names = c(NA, -14L))
    

    reshape2::dcast 提供了一种使数据集“更宽”的简单方法,以便年份形成包含来自total 的值的列:

    dcast(df, agecat + Drug ~ year, value.var = "total")
    
    #   agecat          Drug 1999 2000
    # 1     <1      Amikacin   12    3
    # 2     <1      Cefepime    3    6
    # 3     <1   Ceftazidime   13    3
    # 4     <1 Ciprofloxacin   NA    4
    # 5    1-5      Amikacin   37   52
    # 6    1-5      Cefepime   25   34
    # 7    1-5   Ceftazidime   38   45
    # 8    1-5 Ciprofloxacin   38   NA
    

    但是,根据 cmets 的说法,实际数据集略有不同。在给定的年份中,它似乎对agecatdrug 有不止一个观察结果。因此,对于每个agecat-drug-year 组合,total 的值不止一个。由于需要以某种方式汇总这些值,reshape2 抱怨道:

    缺少聚合函数:默认为长度

    解决方案是聚合这些值以获得total 在给定年份中agecatdrug 的总和。这是通过设置fun.aggregate = sum

    dcast(df, agecat + Drug ~ year, value.var = "total", fun.aggregate = sum)
    

    【讨论】:

      猜你喜欢
      • 2019-05-20
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多