【问题标题】:How to create a new data table that groups by variables in R如何在 R 中创建按变量分组的新数据表
【发布时间】:2016-05-17 09:28:26
【问题描述】:

我有一个数据框,其中包含有关患者及其在医院就诊状态的信息。我想从原始数据框中创建一个新的数据框,其中将列出他们的约会次数、错过次数和出席人数

例如,我的起始数据表如下所示:

Patient      Status
A            Attended
A            Attended
A            Attended
B            Did not attend
C            Did not attend
D            Attended
D            Did not attend

我希望输出表看起来像

Patient      Number of appointments  Number attended     Number missed
A            3                              3                   0
B            1                              0                   1 
C            1                              0                   1
D            2                              1                   1

我尝试查看以前的答案,但找不到我的答案。我想我可能不得不对聚合函数做一些事情,但我想不通

【问题讨论】:

  • 理想情况下,您需要自己尝试一些东西。如果您不知道如何,请先尝试 google。然后尝试解决问题。如果您的代码仍然不起作用....然后在此处发布问题。包括对问题的清晰描述。包括不起作用的代码 - 请参见此处 最小、完整和可验证的示例 - 告诉我们您期望什么行为以及您实际得到什么 - 包括任何错误并告诉我们在哪里他们是。干杯:)
  • 谢谢大卫,下次我会这样做

标签: r data.table aggregate dplyr


【解决方案1】:

我们可以的

library(data.table)
setDT(df1)[,.(Number_of_appointments = .N, 
              Number_Attended = sum(Status== "Attended"),
              Number_Missed = sum(Status == "Did not attend")) , Patient]

或使用dcast

dcast(setDT(df1)[, N:=.N, Patient], Patient+N~Status, value.var='Status', length)
#   Patient N Attended Did not attend
#1:       A 3        3              0
#2:       B 1        0              1
#3:       C 1        0              1
#4:       D 2        1              1

【讨论】:

    【解决方案2】:
    abc <- data.frame(Patient = c(rep("A",3),"B","C",rep("D",2)), 
    Status = c(rep("Attended",3),
      rep("Did not attend",2),"Attended","Did not attend"))
    
    abc[,c("Patient","Status")] <-apply(abc[,c("Patient","Status")] ,2, as.character)
    
      Patient         Status
           A       Attended
           A       Attended
           A       Attended
           B Did not attend
           C Did not attend
           D       Attended
           D Did not attend
    
    
    for( i in unique(abc$Patient)){
      abc$Number_of_appointments[abc$Patient==i] <-length(abc$Status[abc$Patient==i])
      abc$Number_attended[abc$Patient==i] <- sum(grepl("Attended",abc$Status[abc$Patient==i])%in%"TRUE")
      abc$Number_missed[abc$Patient==i] <- sum(!grepl("Attended",abc$Status[abc$Patient==i])%in%"TRUE")
    }
    
    abc$Status <-NULL
    abc1<-abc[!duplicated(abc$Patient),]
    
      Patient         Status Number_of_appointments Number_attended Number_missed
           A       Attended                      3               3             0
           B Did not attend                      1               0             1
           C Did not attend                      1               0             1
           D       Attended                      2               1             1
    

    【讨论】:

      猜你喜欢
      • 2021-06-13
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      • 2018-05-22
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      相关资源
      最近更新 更多