我有以下生存分析数据框。ID
是主题 ID,event
是事件的发生与否,以及time
每次观察的时间。
test.df<-data.frame(expand.grid(id=c("A","B","C","D","E"),event=c(0,0,1,0,1,0,0,1,0,1,0,1,1,0,0,1,0,1,0,0,1,0,1,0,1,1,0,0,1,0,1)))
test.df$time=round(rnorm(155,5,1),1)
我想将数据框减少到以下内容:对于每个 id,所有行,直到(但包括)第一次出现 event=1。每行(无论 event=0 或 event=1),在每个 ID 中的第一个 event=1之后都不应该出现在缩减的数据帧中。
请不要那样length(unique(test.df$time))<length(test.df$time)
(如果使用ave()
解决方案的情况下)。
也可能存在第一个观察结果是 event=1 的 ID,因此它们根本不会包含在缩减的数据框中。
有没有很好的方法来做到这一点?
我试过test.df$cumsum<-ave(test.df$event, test.df$id, cumsum)
了,但似乎没有像我预期的那样工作。
编辑:这是一个具有 DWin 要求的 data.frame
test.df<-structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L,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.3, 3.7, 4, 4.1, 4.2, 4.3, 4.4, 4.5,
4.6, 4.6, 4.7, 4.8, 4.9, 4.9, 4.9, 5.1, 5.2, 5.5, 5.5, 5.7,
5.7, 5.8, 5.8, 5.9, 6, 6, 6, 6.4, 6.7, 6.8, 2.9, 3, 3.6,
3.6, 3.8, 4.1, 4.4, 4.5, 4.6, 4.6, 4.6, 4.6, 4.7, 4.8, 5,
5, 5, 5.1, 5.2, 5.3, 5.5, 5.5, 5.5, 5.7, 5.9, 5.9, 5.9, 5.9,
6.1, 6.2, 6.7)), .Names = c("id", "event", "time"), row.names = c(101L,
1L, 146L, 141L, 106L, 151L, 66L, 111L, 131L, 6L, 91L, 121L, 21L,
46L, 16L, 26L, 86L, 56L, 76L, 31L, 136L, 41L, 61L, 126L, 36L,
71L, 81L, 11L, 51L, 116L, 96L, 107L, 112L, 67L, 72L, 2L, 27L,
77L, 92L, 127L, 47L, 122L, 32L, 82L, 117L, 97L, 132L, 17L, 42L,
57L, 152L, 147L, 37L, 137L, 12L, 102L, 7L, 52L, 87L, 62L, 22L,
142L, 128L, 8L, 18L, 113L, 138L, 3L, 78L, 13L, 48L, 73L, 108L,
143L, 38L, 148L, 68L, 153L, 98L, 23L, 63L, 118L, 53L, 88L, 93L,
103L, 83L, 33L, 58L, 133L, 43L, 123L, 28L, 44L, 109L, 94L, 144L,
104L, 89L, 114L, 129L, 59L, 39L, 124L, 19L, 29L, 54L, 69L, 139L,
14L, 84L, 9L, 134L, 4L, 74L, 24L, 64L, 34L, 49L, 79L, 149L, 119L,
154L, 99L, 120L, 135L, 125L, 150L, 35L, 15L, 5L, 20L, 40L, 65L,
80L, 155L, 60L, 145L, 10L, 30L, 50L, 95L, 140L, 90L, 110L, 115L,
130L, 70L, 25L, 55L, 75L, 100L, 105L, 45L, 85L), class = "data.frame")