r-如何标记患者将在30天后再次入院的事件



我有一个关于患者发作的数据集。每个病人都有自己的病人个人号码。住院事件有入院日期和出院日期。我需要在新的变量中(用TRUE或1(标记该事件中的患者将在30天内再次入院的所有事件。

install.packages("lubridate")
library(lubridate)
admission <- c("06/23/2013", "06/30/2013", "07/12/2013","06/24/2013","06/28/2013","06/29/2013","06/23/2013","06/24/2013","06/24/2013","07/02/2013","07/09/2013","06/24/2013","09/08/2013","07/22/2014")
discharge<- c("06/25/2013", "07/03/2014", "07/17/2014","06/30/2013","06/30/2013","07/02/2013","06/29/2013","06/29/2013","06/27/2013","07/05/2013","07/12/2013","06/28/2013","10/12/2013","08/01/2014")
admission.date <- mdy(admission)
discharge.date <- mdy(discharge)
patientPersonalNumber<-c("001","002","004","005","006","007","008","009","010", "005","005","011","005", "004")
df<-data.frame(patientPersonalNumber,admission.date,discharge.date)
df

patientPersonalNumber admission.date discharge.date
1                    001     2013-06-23     2013-06-25
2                    002     2013-06-30     2014-07-03
3                    004     2014-07-12     2014-07-17
4                    005     2013-06-24     2013-06-30
5                    006     2013-06-28     2013-06-30
6                    007     2013-06-29     2013-07-02
7                    008     2013-06-23     2013-06-29
8                    009     2013-06-24     2013-06-29
9                    010     2013-06-24     2013-06-27
10                   005     2013-07-02     2013-07-05
11                   005     2013-07-09     2013-07-12
12                   011     2013-06-24     2013-06-28
13                   005     2013-09-08     2013-10-12
14                   004     2014-07-22     2014-08-01
So I have to mark lines (3,4,10) as true.
#4 Patient 005 discharged 2013-06-30 was admitted 2013-07-02
#10 Patient 005 discharged 2013-07-05 was admitted 2013-07-09
#3 Patient 004 discharged 2013-06-30 was admitted 2013-07-22
I appreciate any help.
#origianl data were edit

会使用这样的东西:

require(tidyverse)
df %>% 
arrange(patientPersonalNumber, admission.date) %>% 
group_by(patientPersonalNumber) %>% 
mutate(re.admin = (lag(discharge.date) + 30) >= admission.date) %>% 
mutate(re.admin = ifelse(is.na(re.admin), FALSE, re.admin ))

# A tibble: 14 x 4
# Groups:   patientPersonalNumber [10]
patientPersonalNumber admission.date discharge.date re.admin
<chr>                 <date>         <date>         <lgl>   
1 001                   2013-06-23     2013-06-25     FALSE   
2 002                   2013-06-30     2014-07-03     FALSE   
3 004                   2013-07-22     2014-08-01     FALSE   
4 004                   2014-07-12     2014-07-17     TRUE    
5 005                   2013-06-24     2013-06-30     FALSE   
6 005                   2013-07-02     2013-07-05     TRUE    
7 005                   2013-07-09     2013-07-12     TRUE    
8 005                   2013-09-08     2013-10-12     FALSE   
9 006                   2013-06-28     2013-06-30     FALSE   
10 007                   2013-06-29     2013-07-02     FALSE   
11 008                   2013-06-23     2013-06-29     FALSE   
12 009                   2013-06-24     2013-06-29     FALSE   
13 010                   2013-06-24     2013-06-27     FALSE   
14 011                   2013-06-24     2013-06-28     FALSE   

相关内容

  • 没有找到相关文章

最新更新