上下文:据我所知,R 缺乏一致的函数,这些函数有助于在生存/事件历史分析的上下文中进行数据准备,例如将事件拆分以包括时变协变量(有时称为“计数过程数据”)。
对于每个人 ( id
),给出了每集的开始 ( start.cp
) 和结束时间 ( stop.cp
)。此外,对于1,2, ..., p时变协变量 (TVC) 中的每一个,我们知道剧集何时开始 ( tvc.start_
) 和何时结束 ( tvc.stop_
)。
在我的示例中(见下文),TVC 的数量为 2,但通常数量可以变化(从 1 到 p)。
例子:
输入数据:
id start.cp stop.cp tvc.start1 tvc.start2 tvc.stop1 tvc.stop2
1 1 1 2 2 3 4 7
2 1 2 3 2 3 4 7
3 1 3 4 2 3 4 7
4 1 4 7 2 3 4 7
5 1 7 12 2 3 4 7
structure(list(id = c(1, 1, 1, 1, 1), start.cp = c(1, 2, 3, 4,
7), stop.cp = c(2, 3, 4, 7, 12), tvc.start1 = c(2, 2, 2, 2, 2
), tvc.start2 = c(3, 3, 3, 3, 3), tvc.stop1 = c(4, 4, 4, 4, 4
), tvc.stop2 = c(7, 7, 7, 7, 7)), .Names = c("id", "start.cp",
"stop.cp", "tvc.start1", "tvc.start2", "tvc.stop1", "tvc.stop2"),
row.names = c(NA, 5L), class = "data.frame")
TVC 的名称是已知的,即在此示例中,已知
tvc.start <- c("tvc.start1", "tvc.start2")
tvc.stop <- c("tvc.stop1", "tvc.stop2")
预期成绩:
id start.cp stop.cp tvc.start1 tvc.start2 tvc.stop1 tvc.stop2 tvc.d1 tvc.d2
1 1 1 2 2 3 4 7 0 0
2 1 2 3 2 3 4 7 1 0
3 1 3 4 2 3 4 7 1 0
4 1 4 7 2 3 4 7 0 1
5 1 7 12 2 3 4 7 0 1
structure(list(id = c(1, 1, 1, 1, 1), start.cp = c(1, 2, 3, 4,
7), stop.cp = c(2, 3, 4, 7, 12), tvc.start1 = c(2, 2, 2, 2, 2
), tvc.start2 = c(3, 3, 3, 3, 3), tvc.stop1 = c(4, 4, 4, 4, 4
), tvc.stop2 = c(7, 7, 7, 7, 7), tvc.d1 = c(0, 1, 1, 0, 0), tvc.d2 = c(0,
0, 0, 1, 1)), .Names = c("id", "start.cp", "stop.cp", "tvc.start1",
"tvc.start2", "tvc.stop1", "tvc.stop2", "tvc.d1", "tvc.d2"), row.names = c(NA,
5L), class = "data.frame")
问题:对于每个 TVC,我想创建一个新向量(tvc.d1
, tvc.d2
,参见示例),它表示给定的剧集(由start.cp
和定义stop.cp
)与 TVC 的间隔重叠(=1)。假设[start.cp, stop.cp)。如何在不循环 TVC 集的情况下做到这一点,即我正在寻找矢量化解决方案。
PS:请随意更改标题...