Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to generate a variable that corresponds to multiple columns?

$
0
0

I have a data set for 10 patients that looks like this:

df = data.frame(id=c(1:10),
               event1=sample(1:4, 10, replace=T),
               date1=Sys.Date() - sample(100:500, 10, replace=T), 
               event2=sample(1:4, 10, replace=T),
               date2=Sys.Date() - sample(100:500, 10, replace=T), 
               event3=sample(1:4, 10, replace=T),
               date3=Sys.Date() - sample(100:500, 10, replace=T),
               follow_up=Sys.Date() - sample(10:100, 10, replace=T)) 

Each patient has multiple events, and each event has its corresponding date. An event and date might also be NA if it did not occur. The follow-up date is the last date the patient was checked for events.

I created a variable for outcome bleeding (in this case if an event == 2) as follows:

all_vars_quo <- quo(c(event1, event2, event3))

df %>% rowwise() %>% 
  mutate(bleeding_no = sum(!!all_vars_quo==2, na.rm=TRUE)) %>% 
  mutate(bleeding = if_else(bleeding_no>0,1,0))

Now, I would like to add an additional variable "bleeding_date" with the corresponding date of the bleeding. In case more than one bleeding occurred in the same patient, I would like to use the date that is farthest in the past. In case no bleeding occurred, I would like to use the date at follow-up. So far, I was unable to come up with a working piece of code... Any help is much appreciated!


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>