Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep the second occurrence in a column in R

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.

like image 743
Lb93 Avatar asked Dec 20 '22 02:12

Lb93


1 Answers

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]
like image 181
David Arenburg Avatar answered Jan 12 '23 10:01

David Arenburg