计算每个患者的持续时间

时间:2019-03-21 22:18:06

标签: r

     patient.id       date type 
5           1053 2006/12/14   DX    
2           1053  2007/4/21  HSCT

1           1053  2007/5/29   FU  
6           1053  2007/7/20   FU  
3           1053  2007/9/20   FU   
4           1053 2007/11/18   D1  
7           1138   2009/9/3   DX  
13          1138   2010/2/3 HSCT  
23          1138  2010/3/11   FU  
10          1138   2010/6/6   FU   
9           1138  2010/8/31   FU   
15          1138  2010/11/5   FU   
11          1138   2011/2/7   FU   
16          1138  2011/5/15   FU   
17          1138  2011/7/18   FU   
14          1138  2011/9/21   FU   
24          1138 2011/12/13   FU   
19          1138  2012/3/13   FU   
25          1138  2012/5/11   D1

2 个答案:

答案 0 :(得分:1)

R基础解决方案:

> lapply(with(dat, split(date, patient.id)), function(x) diff(range(x)))
$`1053`
Time difference of 339 days

$`1138`
Time difference of 981 days

答案 1 :(得分:0)

使用dplyr转换为日期格式,然后按患者分组并计算max(date)-min(date)。

library(dplyr)
mydata %>% 
  mutate(date = as.Date(date, "%Y/%m/%d")) %>% 
  group_by(patient.id) %>% 
  summarise(Survival = as.numeric(max(date) - min(date)))

结果:

  patient.id Survival
       <int>    <dbl>
1       1053      339
2       1138      981