Reputation: 297
I am working on patient health records and trying to find the last clinic visit from there first clinic visit, Below is the data frame :-
patient_id admit_date
P1 05/1/2015
P2 09/10/2015
P1 08/5/2015
P1 09/10/2015
P2 12/1/2015
The output should be :-
patient_id admit_date last_visit
P1 05/1/2015 NA
P2 09/10/2015 NA
P1 08/5/2015 05/1/2015
P1 09/10/2015 08/5/2015
P2 12/1/2015 09/10/2015
I was planning on using the dplyr approach :-
raw_data %>%
group_by(patient_id) %>% mutate(last_visit= )
I need some guidance in the mutate part.
Upvotes: 2
Views: 53
Reputation: 24945
First, convert the date format to a proper date, then use lag
to go one time period back:
library(dplyr)
rawdata %>% group_by(patient_id) %>%
mutate(admit_date = as.Date(admit_date, format = "%d/%m/%Y"),
last_visit = lag(admit_date))
Source: local data frame [5 x 3]
Groups: patient_id [2]
patient_id admit_date last_visit
(fctr) (date) (date)
1 P1 2015-01-05 <NA>
2 P2 2015-10-09 <NA>
3 P1 2015-05-08 2015-01-05
4 P1 2015-10-09 2015-05-08
5 P2 2015-01-12 2015-10-09
Your dates are ambiguous - if you have d-m-y this is correct, otherwise change the format argument to "%m/%d/%y"
Upvotes: 3