如何解决使用一个数据集上一列中的日期设置一年范围向前,以在 R 中与 id 匹配的不同数据集上查找日期
我有两个数据集,(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
我希望决赛桌看起来像这样
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,3,3),lat=c("le","re","le","NA","bilat"),year= c(18,11,15,10,13),status=status <- c(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,lat=c("NA","57",outcome = c(0,date= dates2,year= c(15,19,08,12,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,units= "days"))][,outcome:=fifelse(is.na(outcome),outcome)]
# add flag to show whether the part of da2 (id,lat) also appears in dt2
setkey(da2,id,lat)
setkey(dt2,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,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,lat1op,year1op,date2event)]
我和你的结果之间的差异位于第 5 行。 从您提供的数据中,我找不到任何带有 NA 的 end_date。 至于prior_outcome,您没有说明如何计算它。我认为这不是主要问题。
,这里有一个使用非对等连接的选项:
cols <- c("outcome","end_date")
dt1[,oneyr := date + 365L] #or in case of leap year,dt1[,oneyr := as.Date(sapply(date,function(d) seq(d,by="1 year",length.out=2L)[[2L]]),origin="1970-01-01")]
dt1[,(cols) :=
dt2[.SD,on=.(id,date>=date,date<=oneyr),mult="first",.(x.outcome,x.date)]
]
dt1[is.na(outcome),outcome := 0L]
输出:
id lat year status date oneyr outcome end_date
1: 1 le 18 1 2018-07-06 2019-07-06 1 2019-04-03
2: 1 re 11 1 2011-04-12 2012-04-11 1 2011-07-14
3: 2 le 15 0 2015-01-10 2016-01-10 0 2015-11-10
4: 2 re 11 0 2011-07-20 2012-07-19 0 2012-04-23
5: 3 NA 10 1 2010-02-18 2011-02-18 0 <NA>
6: 3 57 13 0 2013-09-26 2014-09-26 1 2014-01-15
qn 更新后编辑。不清楚新的所需输出是什么,您可以尝试以下操作:
dt1[,oneyr := date + 365L]
cols <- paste0("i.",names(dt1))
a1 <- dt2[dt1,c(mget(cols),.(outcome=outcome,end_date=x.date))]
setnames(a1,names(a1),gsub("^i.","",names(a1)))
a2 <- dt2[a1[is.na(outcome)],end_date=x.date))]
setnames(a2,names(a2),names(a2)))
setorder(rbindlist(list(a1[!is.na(outcome)],a2),use.names=TRUE),date)[]
输出:
id lat year status date oneyr outcome end_date
1: 1 re 11 1 2011-04-12 2012-04-11 1 2011-07-14
2: 1 le 18 1 2018-07-06 2019-07-06 0 2019-04-03
3: 1 le 18 1 2018-07-06 2019-07-06 1 2019-04-30
4: 2 re 11 0 2011-07-20 2012-07-19 0 2012-04-23
5: 2 le 15 0 2015-01-10 2016-01-10 0 2015-11-10
6: 2 le 15 0 2015-01-10 2016-01-10 0 2015-02-18
7: 3 NA 10 1 2010-02-18 2011-02-18 NA <NA>
8: 3 57 13 0 2013-09-26 2014-09-26 0 2014-01-15
在所需的输出更新后编辑:
cols <- c("outcome",by=.EACHI,{
w <- which(outcome==1L)
if (length(w) > 0L) {
.(outcome=outcome[w[1L]],x.date[w[1L]])
} else {
.(outcome=outcome[1L],x.date[1L])
}
}][,(1L:4L) := NULL]
]
dt1[is.na(outcome),(1L:3L) := NULL]
]
dt1[is.na(outcome),outcome := 0L][]
输出:
id lat year status date oneyr outcome end_date
1: 1 le 18 1 2018-07-06 2019-07-06 1 2019-04-30
2: 1 re 11 1 2011-04-12 2012-04-11 1 2011-07-14
3: 2 le 15 0 2015-01-10 2016-01-10 0 2015-02-18
4: 2 re 11 0 2011-07-20 2012-07-19 0 2012-04-23
5: 3 NA 10 1 2010-02-18 2011-02-18 0 <NA>
6: 3 bilat 13 1 2013-09-26 2014-09-26 1 2014-02-28
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。