使用R 3.6版中的面板数据统计患病人数

hsgswve4  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(80)

我有面板数据类似于下面的地方有多个行每个patid。我想创建第四列,如果patid在发行日期之前至少有一个event_date,在发行日期之后至少有一个event_date,则该列等于1,否则为0(即,每个PatiD需要两个事件日期之间的发布日期)。

mydata <- read.table(header=T, 
text="
patid issue_date event_date 
1 01/09/2014 03/12/2013 
1 01/09/2014 26/09/2006 
1 01/09/2014 09/09/2011 
2 27/09/2012 04/02/2013 
2 27/09/2012 30/03/2010 
2 27/09/2012 29/10/2008 
2 27/09/2012 22/10/2014 
3 02/07/2002 04/07/2007 
3 02/07/2002 24/06/2009 
3 02/07/2002 06/02/2007 
3 02/07/2002 23/08/2013 
3 02/07/2002 17/11/2015 
4 05/05/2014 19/09/2013 
4 05/05/2014 05/10/2010 
4 05/05/2014 20/04/2011 
4 05/05/2014 12/04/2016 
4 05/05/2014 12/05/2015" 
)

下面是我想要的。

mydata <- read.table(header=T, 
text="
patid issue_date event_date 
1 01/09/2014 03/12/2013 0 
1 01/09/2014 26/09/2006 0 
1 01/09/2014 09/09/2011 0 
2 27/09/2012 04/02/2013 1 
2 27/09/2012 30/03/2010 1
2 27/09/2012 29/10/2008 1
2 27/09/2012 22/10/2014 1
3 02/07/2002 04/07/2007 0
3 02/07/2002 24/06/2009 0
3 02/07/2002 06/02/2007 0
3 02/07/2002 23/08/2013 0
3 02/07/2002 17/11/2015 0
4 05/05/2014 19/09/2013 1
4 05/05/2014 05/10/2010 1
4 05/05/2014 20/04/2011 1
4 05/05/2014 12/04/2016 1
4 05/05/2014 12/05/2015 1" 
)

以下是我尝试过的

mydata$event_dt<-as.Date(mydata$event_date)
mydata$issue_date<-as.Date(mydata$issue_date)

class(mydata$event_dt)
class(mydata$issue_date)

mydata %>% group_by(eid) %>%
   mutate(new = case_when(any((issue_date >= event_date) & (issue_date < event_date)) ~ 
   1, TRUE ~ 0))

这似乎对每个人来说都是0。我觉得情况可能不对。

e37o9pze

e37o9pze1#

library(dplyr)

mydata <- mydata %>%
  mutate(event_dt = as.Date(event_date, format = "%d/%m/%Y"),
         issue_dt = as.Date(issue_date, format = "%d/%m/%Y")) %>%
  group_by(patid) %>%
  mutate(new = as.numeric(any(event_dt < issue_dt) & any(event_dt > issue_dt))) %>%
  select(-event_dt, -issue_dt)

print(mydata)

A tibble: 17 × 4
# Groups:   patid [4]
   patid issue_date event_date   new
   <int> <chr>      <chr>      <dbl>
 1     1 01/09/2014 03/12/2013     0
 2     1 01/09/2014 26/09/2006     0
 3     1 01/09/2014 09/09/2011     0
 4     2 27/09/2012 04/02/2013     1
 5     2 27/09/2012 30/03/2010     1
 6     2 27/09/2012 29/10/2008     1
 7     2 27/09/2012 22/10/2014     1
 8     3 02/07/2002 04/07/2007     0
 9     3 02/07/2002 24/06/2009     0
10     3 02/07/2002 06/02/2007     0
11     3 02/07/2002 23/08/2013     0
12     3 02/07/2002 17/11/2015     0
13     4 05/05/2014 19/09/2013     1
14     4 05/05/2014 05/10/2010     1
15     4 05/05/2014 20/04/2011     1
16     4 05/05/2014 12/04/2016     1
17     4 05/05/2014 12/05/2015     1

相关问题