如何在R中的日期变量前后保持一年随访时间的行,以确保患者在整个时间内都得到随访



对于这个问题,您需要复制下面的代码来获得df。我有5名患者的重复患者数据。每一行代表一次咨询,咨询日期为(consdate(。每位患者的随访时间为从regstartdate到end_date。我感兴趣的日期是portal_reg_date。我希望只保留符合以下条件的行:(1( 保留在portal_reg_date前1年发生消费日期的行(2( 保留在portal_reg_date后1年发生consdate的行(3( 在那些满足条件(1(和(2(的行中,只保留在portal_reg_date之前和之后1年的行,该行在regstartdate和end_date之内[所以只保留在portal_reg_date 之前和之后跟踪1年的patid

dfr <- tibble::tribble(
~patid, ~consdate, ~portal_reg_date, ~regstartdate, ~end_date,
1, "2020-02-26", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2015-06-19", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-06-28", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2020-03-02", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-04-11", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-06-02", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2018-05-23", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2018-05-21", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2018-06-18", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-04-08", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-06-24", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2020-10-17", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-07-03", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2011-06-02", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-02-10", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2013-12-09", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-01-31", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-09-30", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2021-05-17", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-11-04", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2011-04-27", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-01-07", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-01-31", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-03-13", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2016-12-22", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-09-01", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-07-31", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-02-03", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-01-10", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2018-10-02", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-10-30", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-02-24", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2016-12-21", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-02-28", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-05-20", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-04-08", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2018-01-12", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-06-06", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2018-10-31", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-03-11", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2016-12-08", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2018-07-13", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-05-09", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-03-20", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-10-29", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-02-27", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2015-10-05", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-05-06", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-05-09", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-09-12", "2017-06-19", "2010-10-06", "2021-08-15"
)

例如,以下是符合条件的行:

*如果consdate在:2020-02-26,而portal_reg_date在:2017-06-19,则不符合条件,因为consdate在portal_reg_date之后超过一年

*如果消费日期为:2017-04-11,而portal_reg_date为:2017-06-19,则符合条件,因为消费日期在portal_ret_date之前1年内。regstartdate在2010-10-06,end_date在2021-08-15,包括consdate和portal_reg_date。

*如果consdate在:2018-06-18,portal_reg_date在:2017-06-19,这将符合条件,因为consdate在portal_reg_date1年内,regstartdate在2010-10-06,end_date在2021-08-15,其中包括consdate和portal_reg_day。

这里有一个使用tidyverse中一些动词的解决方案。需要更少的行来重现问题(这里是50行(,这也简化了对问题的理解。

# To export the data, use head to fetch few rows
# + converting date as character
# dfr_raw <- head(df %>% mutate(across(where(is.Date), as.character)), 50)
# datapasta::tribble_paste(dfr_raw)

dfr <- tibble::tribble(
~patid, ~consdate, ~portal_reg_date, ~regstartdate, ~end_date,
1, "2020-02-26", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2015-06-19", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-06-28", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2020-03-02", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-04-11", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-06-02", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2018-05-23", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2018-05-21", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2018-06-18", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-04-08", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-06-24", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2020-10-17", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-07-03", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2011-06-02", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-02-10", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2013-12-09", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-01-31", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-09-30", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2021-05-17", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-11-04", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2011-04-27", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-01-07", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-01-31", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-03-13", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2016-12-22", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-09-01", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-07-31", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-02-03", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-01-10", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2018-10-02", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-10-30", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-02-24", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2016-12-21", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-02-28", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-05-20", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-04-08", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2018-01-12", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-06-06", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2018-10-31", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-03-11", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2016-12-08", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2018-07-13", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2017-05-09", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-03-20", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-10-29", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-02-27", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2015-10-05", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2014-05-06", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-05-09", "2017-06-19", "2010-10-06", "2021-08-15",
1, "2019-09-12", "2017-06-19", "2010-10-06", "2021-08-15"
)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
dfr <- dfr %>% mutate_at(-1, as_date)
treshlod <- dyears(1)
dfr %>%
arrange_all() %>%
mutate(diff = consdate - portal_reg_date) %>%
filter(
consdate - treshlod < portal_reg_date & portal_reg_date < consdate + treshlod,
regstartdate < consdate & consdate < end_date
)
#> # A tibble: 15 × 6
#>    patid consdate   portal_reg_date regstartdate end_date   diff     
#>    <dbl> <date>     <date>          <date>       <date>     <drtn>   
#>  1     1 2016-12-08 2017-06-19      2010-10-06   2021-08-15 -193 days
#>  2     1 2016-12-21 2017-06-19      2010-10-06   2021-08-15 -180 days
#>  3     1 2016-12-22 2017-06-19      2010-10-06   2021-08-15 -179 days
#>  4     1 2017-01-10 2017-06-19      2010-10-06   2021-08-15 -160 days
#>  5     1 2017-01-31 2017-06-19      2010-10-06   2021-08-15 -139 days
#>  6     1 2017-02-10 2017-06-19      2010-10-06   2021-08-15 -129 days
#>  7     1 2017-02-24 2017-06-19      2010-10-06   2021-08-15 -115 days
#>  8     1 2017-03-13 2017-06-19      2010-10-06   2021-08-15  -98 days
#>  9     1 2017-04-11 2017-06-19      2010-10-06   2021-08-15  -69 days
#> 10     1 2017-05-09 2017-06-19      2010-10-06   2021-08-15  -41 days
#> 11     1 2017-10-30 2017-06-19      2010-10-06   2021-08-15  133 days
#> 12     1 2018-01-12 2017-06-19      2010-10-06   2021-08-15  207 days
#> 13     1 2018-05-21 2017-06-19      2010-10-06   2021-08-15  336 days
#> 14     1 2018-05-23 2017-06-19      2010-10-06   2021-08-15  338 days
#> 15     1 2018-06-18 2017-06-19      2010-10-06   2021-08-15  364 days

创建于2022-09-28,reprex v2.0.2

相关内容

  • 没有找到相关文章

最新更新