mardi 13 août 2019

How to create new variable based on time and preexisting variables?

I have a dataset with repeated measurements on multiple individuals over time. It looks something like this:

   ID           Time    Event      
   1   Jan 1 2012, 4pm    Abx            
   1   Jan 2 2012, 2pm   Test            
   1   Jan 26 2012 3 pm  Test            
   1  Jan 29 2012 10 pm   Abx            
   1  Jan 30 2012, 3 pm  Test            
   1    Jan 5 2012 3 pm  Test            
   2   Jan 1 2012, 4pm    Abx           
   2   Jan 2 2012, 2pm   Test            
   2   Jan 26 2012 3 pm  Test            

The dataset is currently based around events. It will later be filtered down to just tests. What I need to do is make a new variable that is 1 when certain events (Abx, in this case) occur within a certain time range of tests. So if the event 'Abx' occurs within, let's say, 48 hours of a Test event, the new variable should equal 1. Otherwise, it should equal zero. I'm hoping to produce something like this:

   ID           Time    Event      New_variable
   1   Jan 1 2012, 4pm    Abx            1
   1   Jan 2 2012, 2pm   Test            1
   1   Jan 26 2012 3 pm  Test            0
   1  Jan 29 2012 10 pm   Abx            1
   1  Jan 30 2012, 3 pm  Test            1
   1    Jan 5 2012 3 pm  Test            0
   2   Jan 1 2012, 4pm    Abx            1
   2   Jan 2 2012, 2pm   Test            1
   2   Jan 26 2012 3 pm  Test            0

I know that I could probably solve this with a combination of Dplyr mutate functions combined with ifelse statements, and if I just wanted to make a variable that reads "1" when the antibiotic event occurs I could do that like this:

 test %>%
   mutate(New_variable = ifelse(Event == 'Abx', 1, 0)) -> test2

But I don't know how to factor in time so that Test events = 1 within 48 hours of an Abx event. I also am not sure how to make sure that the condition is applied only within the same ID. How can I do this?

Any help is appreciated!

Aucun commentaire:

Enregistrer un commentaire