简体   繁体   中英

Select date from a previous observation and input it into a new column in R

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.

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 dmy this is correct, otherwise change the format argument to "%m/%d/%y"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM