我有一个这样的数据集:
id status time event
<chr> <dbl> <dbl> <time>
1 22016890 1 459 425 days
2 22042190 1 83 NA days
3 18108887 1 373 NA days
4 17706799 1 33 NA days
5 17039253 1 531 NA days
6 23223163 1 230 NA days
7 15848065 1 1 NA days
,我需要创建一个与时间相关的协变量event
。我该怎么做?
我尝试使用survival::tmerge
来遵循vignette的示例 3.2 ,但没有成功。
数据集:
nd <- structure(list(id = c("22016890", "22042190", "18108887", "17706799",
"17039253", "23223163", "15848065"), status = c(1, 1, 1, 1, 1,
1, 1), time = c(459, 83, 373, 33, 531, 230, 1), event = structure(c(425,
NA, NA, NA, NA, NA, NA), class = "difftime", units = "days")), row.names = c(NA,
-7L), class = c("tbl_df", "tbl", "data.frame"))
我如何需要我的数据:
# A tibble: 8 x 5
id start end status event
<chr> <dbl> <dbl> <dbl> <dbl>
1 22016890 0 425 1 0
2 22016890 425 459 1 1
3 22042190 0 83 1 0
4 18108887 0 373 1 0
5 17706799 0 33 1 0
6 17039253 0 531 1 0
7 23223163 0 230 1 0
8 15848065 0 1 1 0
答案 0 :(得分:1)
我设法做到这一点:
library(survival)
simpledata <- tmerge(data1 = nd[, c(1:3)], data2 = nd, id = id, tstop = time)
simpledata <- tmerge(simpledata, nd, id=id, infect = tdc(event))
simpledata
id status time tstart tstop infect
1 22016890 1 459 0 425 0
2 22016890 1 459 425 459 1
3 22042190 1 83 0 83 0
4 18108887 1 373 0 373 0
5 17706799 1 33 0 33 0
6 17039253 1 531 0 531 0
7 23223163 1 230 0 230 0
8 15848065 1 1 0 1 0