I have quite a simple dataset:
ID Value Time
1 censored 1
1 censored 2
1 uncensored 3
1 uncensored 4
1 censored 5
1 censored 6
2 censored 1
2 uncensored 2
2 uncensored 3
2 uncensored 4
2 censored 5
I want to keep the first uncensored
occurrence, and I want to keep the first censored
occurrence after an uncensored
one. for example:
ID Value Time
1 uncensored 3
1 censored 5
2 uncensored 2
2 censored 5
Not everyone has their first censored date at time 5, that was just for an example.Value
is a binary variable: 1 for censored, and 0 for uncensored, but I've labelled them.
Here's another possible data.table
solution
library(data.table)
setDT(df1)[, list(Value = c("uncensored", "censored"),
Time = c(Time[match("uncensored", Value)],
Time[(.N - match("uncensored", rev(Value))) + 2L])),
by = ID]
# ID Value Time
# 1: 1 uncensored 3
# 2: 1 censored 5
# 3: 2 uncensored 2
# 4: 2 censored 5
Or similarly, using which
instead of match
setDT(df1)[, list(Value = c("uncensored", "censored"),
Time = c(Time[which(Value == "uncensored")[1L]],
Time[(.N - which(rev(Value) == "uncensored")[1L]) + 2L])),
by = ID]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With