gpt4 book ai didi

r - 使用一个数据集上一列中的日期设置一年范围(向前),以在 R 中与 id 匹配的不同数据集上查找日期

转载 作者:行者123 更新时间:2023-12-04 08:04:38 25 4
gpt4 key购买 nike

我有两个数据集,(dt1) 一个带有“开始”日期,每个 ID 最多两个条目(因为这些是 L 或 R 眼手术的条目)和一个 (dt2) 第二个数据库,其中包含多个日期之前和之后开始日期。这些不仅限于眼科手术,还包括任何其他医疗保健就诊。我想在我的数据集中所有 id 的手术 L 和 R 的开始日期的一年内寻找一个事件。如果他们有结果,我想通过偏向性来匹配他们。如果他们没有,那么在一年内最后一次访问时没有侧向匹配(lat)。先前的结果只是开始日期之前的结果或“事件”的数量,先前结果的总和。

id   lat year status       date
1 le 18 1 2018-07-06
1 re 11 1 2011-04-12
2 le 15 0 2015-01-10
2 re 11 0 2011-07-20
3 NA 10 1 2010-02-18
3 bilat 13 1 2013-09-26



id lat outcome date year
1: 1 NA 0 2015-07-06 15
2: 1 le 0 2019-04-03 19
3: 1 le 1 2019-04-30 19
4: 1 re 1 2011-07-14 11
5: 1 re 1 2015-09-10 15
6: 1 re 1 2008-07-14 8
7: 2 NA 0 2015-11-10 15
8: 2 re 0 2012-04-23 12
9: 2 NA 0 2015-02-18 15
10: 2 57 0 2008-12-01 8
11: 3 57 0 2014-01-15 14
12: 3 NA 0 2014-02-21 14
13: 3 bilat 1 2014-02-28 14
我希望决赛 table 看起来像这样
id lat year status       date outcome   end_date prior_outcome
1 le 18 1 2018-07-06 1 2019-04-30 3
1 re 11 1 2011-04-12 1 2011-07-14 1
2 le 15 0 2015-01-10 0 2015-11-10 0
2 re 11 0 2011-07-20 0 2012-04-23 0
3 NA 10 1 2010-02-18 0 <NA> 0
3 bilat 13 1 2013-09-26 1 2014-02-28 0
这是数据集的代码
 dates <- as.Date(c("2018-07-06","2011-04-12",
"2015-01-10","2011-07-20",
"2010-02-18","2013-09-26"))
dt1 <- data.table(id=c(1,1,2,2,3,3),
lat=c("le","re","le","re","NA", "bilat"),
year= c(18, 11,15,11,10,13),
status=status <- c(1,1,0,0,1,1),
date= dates)

dates2 <- as.Date(c('2015-07-06',
'2019-04-03',
'2019-04-30',
'2011-07-14',
'2015-09-10',
'2008-07-14',
'2015-11-10',
'2012-04-23',
'2015-02-18',
'2008-12-01',
'2014-01-15',
'2014-02-21',
'2014-02-28' ))
dt2 <- data.table(id=c(1,1,1,1,1,1,2,2,2,2,3,3,3),
lat=c("NA","le","le","re","re","re","NA","re","NA","57", "57", "NA","bilat"),
outcome = c(0,0,1,1,1,1,0,0,0,0,0,0,1),
date= dates2, year= c(15,19,19,11,15,08,15,12,15,08,14,14,14))

我尝试过这样的事情,但它在原始集合中不起作用,因为我在开始日期之前得到结果,所以我假设代码可能在这个小数据集中工作,但实际上,它在某种程度上是不正确的。
#left join dt1 and dt2
dt1_dt2 <- left_join( dt2,dt1, by= "id", suffix=c("2event","1op"))

#filter does with outcome after date1
dt1_dt2$tdiff = difftime(dt1_dt2$date2event,dt1_dt2$date1op, units= "days")
dt1_dt2 = dt1_dt2 %>% filter(outcome== 1) %>%
filter(tdiff <= 365, tdiff >= 0)


#then match on the closest date since farthest was not supported
setDT(dt1)
setDT(dt1_dt2)
setkey(dt1, id, lat) #key set to match
dt3 <- dt1_dt2[, date2, by =.(id, lat2), roll ="nearest"] #how can I keep all variables?
dt3 = unique(dt3, by = c("id", "date2","lat2"))

最佳答案

更新的答案

# construct complete data
da <- merge(dt1,dt2,by=c("id","lat"),all.x = TRUE,suffixes = c("_start","_end"))
# select desired columns => add tdiff ==> replace outcome == NA with 0
da2 <- da[,.(id,lat,year_start,status,date_start,outcome,date_end)][
, tdiff := as.numeric( difftime(date_end, date_start, units= "days"))][, outcome:=fifelse(is.na(outcome),0,outcome)]

# add flag to show whether the part of da2 (id,lat) also appears in dt2
setkey(da2,id,lat)
setkey(dt2,id,lat)
da2[,flag:=FALSE][dt2,flag:=TRUE]

# get desired result
dt_desired <- da2[0 <= tdiff & tdiff <= 365 | lat == "NA" | is.na(date_end)]
rows <- dt_desired[flag==FALSE & outcome == 0]
# fill with last event's date_end within one year
dt_desired[flag==FALSE & outcome == 0]$date_end <- dt2[,.SD,keyby=.(id,date)][,.SD[.N],by=id][rows,date]
dt_desired[as.numeric( difftime(date_end, date_start, units= "days")) > 365]$date_end <- NA
结果 :
   id lat year_start status date_start outcome   date_end tdiff  flag
1: 1 le 18 1 2018-07-06 1 2019-04-03 271 TRUE
2: 1 re 11 1 2011-04-12 1 2011-07-14 93 TRUE
3: 2 le 15 0 2015-01-10 0 2015-11-10 NA FALSE
4: 2 re 11 0 2011-07-20 0 2012-04-23 278 TRUE
5: 3 57 13 0 2013-09-26 1 2014-01-15 111 TRUE
6: 3 NA 10 1 2010-02-18 0 <NA> NA FALSE
原答案
根据您的代码,我得到的结果接近您想要的结果。请检查是否正确。
dt1_dt2 <- left_join( dt2,dt1, by= "id", suffix=c("2event","1op"))
# add column tdiff, equal to your method
dt1_dt2[, tdiff := as.numeric( difftime(date2event, date1op, units= "days") )]

# select desired columns
dt1_dt2[0 <= tdiff & tdiff <= 365,
.(id,lat1op,year1op,status,date1op,outcome,date2event)]
我和你的结果之间的差异位于第 5 行。
从你提供的数据中,我找不到任何 结束日期不适用 .
至于 优先结果 ,你没有展示如何计算它。我认为这不是主要问题。
enter image description here

关于r - 使用一个数据集上一列中的日期设置一年范围(向前),以在 R 中与 id 匹配的不同数据集上查找日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66280313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com