【问题标题】:How to calcualte bed discharge rate and number of person in each day?如何计算每天的出院率和人数?
【发布时间】:2021-09-18 21:12:18
【问题描述】:

我想知道如何计算每天的患者人数,它想包括当天的患者出院,但有入院患者。 因此,7/17 不应找到患者 c。

我有一个更大的数据集。这只是一个例子。

谢谢你帮助我。

install.packages("lubridate")
library(lubridate)
admission <- c("06/23/2013", "06/30/2013", "07/12/2014","06/24/2013","06/28/2013","06/29/2013","06/23/2013","06/24/2013","06/24/2013")
discharge<- c("06/25/2013", "07/03/2013", "07/17/2014","06/30/2013","06/30/2013","07/02/2013","06/29/2013","06/29/2013","06/27/2013")
patient<-c("a","b","c","d","e","f","g","h","j")
admission.date <- mdy(admission)
discharge.date <- mdy(discharge)
df<-data.frame(patient,admission.date,discharge.date)
df
  patient admission.date discharge.date
1       a     2013-06-23    2013-06-25
2       b     2013-06-30    2013-07-02
3       c     2014-07-12    2014-07-17
4       d     2013-06-24    2013-06-30
5       e     2013-06-28    2013-06-30
6       f     2013-06-29    2013-07-02
7       g     2013-06-23    2013-06-29
8       h     2013-06-24    2013-06-29
9       j     2013-06-24    2013-06-27
  

【问题讨论】:

    标签: r date time statistics time-series


    【解决方案1】:

    这是一种使用data.table的方法

    library(data.table)
    # set df to data.table format
    setDT(df)
    # Create a table with all dates
    dt.dates <- data.table( date = seq(min(df$admission.date), max(df$discarge.date), by = "1 days") )
    #  perform overlap join
    answer <- df[dt.dates, .(date, patient), on = .(admission.date <= date, discarge.date > date), nomatch = 0L]
    # get unique patients by date
    answer[, .(patients = uniqueN(patient)), by = date]
    #          date patients
    # 1: 2013-06-23        2
    # 2: 2013-06-24        5
    # 3: 2013-06-25        4
    # 4: 2013-06-26        4
    # 5: 2013-06-27        3
    # 6: 2013-06-28        4
    # 7: 2013-06-29        3
    # 8: 2013-06-30        2
    # 9: 2013-07-01        2
    #10: 2013-07-02        1
    #11: 2014-07-12        1
    #12: 2014-07-13        1
    #13: 2014-07-14        1
    #14: 2014-07-15        1
    #15: 2014-07-16        1
    

    【讨论】:

    • 谢谢,这太棒了。你为我节省了很多时间。
    【解决方案2】:

    这是一种使用dplyr的方法:

    library(dplyr)
    
    df %>%
      rowwise() %>%
      do(data.frame(patient=.$patient, Date=seq(.$admission.date,.$discarge.date-1,by="day"))) %>%
      group_by(Date) %>%
      summarize(patients = n())
    

    有输出:

    # A tibble: 15 x 2
       Date       patients
       <date>        <int>
     1 2013-06-23        2
     2 2013-06-24        5
     3 2013-06-25        4
     4 2013-06-26        4
     5 2013-06-27        3
     6 2013-06-28        4
     7 2013-06-29        3
     8 2013-06-30        2
     9 2013-07-01        2
    10 2013-07-02        1
    11 2014-07-12        1
    12 2014-07-13        1
    13 2014-07-14        1
    14 2014-07-15        1
    15 2014-07-16        1
    

    【讨论】:

    • 真的很好。感谢您的帮助。
    • 不要犹豫,标记 Wimpel 或我的答案!
    猜你喜欢
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 2022-10-13
    相关资源
    最近更新 更多