【发布时间】:2025-12-31 16:40:01
【问题描述】:
我有一些时间来处理我正在处理的数据。我想过滤从受试者第一次参加研究到第一次观察到的事件的数据(不担心在第一次事件之后发生的重复事件——只想探索到第一次事件的时间)。
我在filter 函数中使用between,过去它一直对我有用,但这里有问题,因为有些主题从未发生过事件,因此我收到一个错误,指出@ 987654323@
我认为我想要的是一种在开始研究到第一次事件的时间之间过滤数据到对象的方法,或者如果没有事件对象,则该对象的所有数据。
这是我的数据的示例:
## data
subject <- c("A", "A", "A", "A", "B", "B", "C", "C", "C", "D", "E", "E", "E", "E", "E", "F", "F", "F", "F", "F")
event <- c(0,0,1,0,0,0,0,0,1,0,0,1,0,1,1,0,0,0,0,0)
df <- data.frame(subject, event)
## create index to count the days the subject is in the study
library(tidyverse)
df <- df %>%
group_by(subject) %>%
mutate(ID = seq_along(subject))
df
# A tibble: 20 x 3
# Groups: subject [6]
subject event ID
<fct> <dbl> <int>
1 A 0 1
2 A 0 2
3 A 1 3
4 A 0 4
5 B 0 1
6 B 0 2
7 C 0 1
8 C 0 2
9 C 1 3
10 D 0 1
11 E 0 1
12 E 1 2
13 E 0 3
14 E 1 4
15 E 1 5
16 F 0 1
17 F 0 2
18 F 0 3
19 F 0 4
20 F 0 5
## filter event times between the start of the trial and when the subject has the event for the first time
df %>%
group_by(subject) %>%
filter(., between(row_number(),
left = which(ID == 1),
right = which(event == 1)))
最后一部分是我的错误发生的地方。
【问题讨论】:
标签: r tidyverse survival-analysis