I am cleaning up data from a research study. Each subject has a unique study identification number (studyid). They had varying number of visits from 1 to 3. This is what the first two columns look like:
> head(x[1:2])
# A tibble: 6 x 2
studyid visit_name
<fct> <chr>
1 3383-002 screening_visit
2 3383-002 medication_visit
3 3383-002 follow-up_visit
4 3383-007 screening_visit
5 3383-008 medication_visit
6 3383-009 medication_visit
I want to designate the screening_visit as baseline if that is present for that individual subject; if not, designate the medication_visit as baseline, and if not, then designate the follow-up visit as baseline.
I can group_by studyid and obtain groups of up to 3 rows for each subject, but I don't see a way to perform a logical query on those 3 rows simultaneously, return a value and then modify one element of a variable based on the answer.
I can see using mutate but it only works on one row at a time. I also read up about map and other iterative tools but cannot see how they could be applied here. Please help me solve or point me in the direction of reading that might help me.