Creating Event Onset Variable

I have clinical data that records a patient at three time points with a disease outcome indicated by a binary variable. It looks something like this

patientid <- c(100,100,100,101,101,101,102,102,102)
time <- c(1,2,3,1,2,3,1,2,3)
outcome <- c(0,1,1,0,0,1,1,1,0) 

Data<- data.frame(patientid=patientid,time=time,outcome=outcome)
Data

I want to create an onset variable, so for each patient it would code a 1 for the time which the patient first got the disease, but would then be a 0 for any time period before or a time period after (even if that patient still had the disease). For the example data it should now look like this.

patientid <- c(100,100,100,101,101,101,102,102,102)
time <- c(1,2,3,1,2,3,1,2,3)
outcome <- c(0,1,1,0,0,1,1,1,0) 
outcome_onset <- c(0,1,0,0,0,1,1,0,0)

Data<- data.frame(patientid=patientid,time=time,outcome=outcome, 
outcome_onset=outcome_onset)
Data

Therefore I would like some code/ some help automating the creation of the outcome_onset variable.



from Recent Questions - Stack Overflow https://ift.tt/2RWCeqd
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)