How to extract date based on condition over two different variables in R -


i have dataset of 100 observations contain patient id,drugcode,prescription date. want create new column "index date" date when patient changed drug third time.

patientid   drugcode    prescriptiondate    a1  3   07-08-2014    a1  3   08-09-2014    a1  7   19-09-2014    a1  5   30-09-2014   a2  4   11-07-2014   a2  4   21-07-2014   a2  3   13-08-2014   a2  5   26-08-2014   a2  5   30-09-2014   a3  2   16-08-2014   a3  3   17-09-2014   a4  5   08-06-2014   a4  5   29-06-2014   a4  6   20-08-2014   a4  6   24-09-2014   a4  4   22-10-2014   a4  4   25-10-2014    

the data set should this:

patientid   drugcode    prescriptiondate    indexdate   a1  3   07-08-2014  30-09-2014   a1  3   08-09-2014  30-09-2014   a1  7   19-09-2014  30-09-2014   a1  5   30-09-2014  30-09-2014   a2  4   11-07-2014  26-08-2014   a2  4   21-07-2014  26-08-2014   a2  3   13-08-2014  26-08-2014   a2  5   26-08-2014  26-08-2014   a2  5   30-09-2014  26-08-2014   a3  2   16-08-2014  na   a3  3   17-09-2014  na   a4  5   08-06-2014  22-10-2014   a4  5   29-06-2014  22-10-2014   a4  6   20-08-2014  22-10-2014   a4  6   24-09-2014  22-10-2014   a4  4   22-10-2014  22-10-2014   a4  4   25-10-2014  22-10-2014  

in above case,patient a1 & a2 changed drug third time drug 5 on 30-09-2014 , 26-08-2014 respectively;a3 have not changed drug third time , a4 has changed drug 4 on 22-10-2014, index date should 30-09-2014,26-08-2014,na,22-10-2014 respectively.

please if can assist in writing code such problem.

here's base r solution, shamelessly stealing pierre lafortune's brilliant match-unique idea:

df <- data.frame(patientid=c('a1','a1','a1','a1','a2','a2','a2','a2','a2','a3','a3','a4','a4','a4','a4','a4','a4'),drugcode=c(3,3,7,5,4,4,3,5,5,2,3,5,5,6,6,4,4),prescriptiondate=as.date(c('07-08-2014','08-09-2014','19-09-2014','30-09-2014','11-07-2014','21-07-2014','13-08-2014','26-08-2014','30-09-2014','16-08-2014','17-09-2014','08-06-2014','29-06-2014','20-08-2014','24-09-2014','22-10-2014','25-10-2014'),'%d-%m-%y')); df$indexdate <- do.call('c',by(df,df$patientid,function(g) rep(g$prescriptiondate[match(unique(g$drugcode)[3],g$drugcode)],nrow(g)))); df; ##    patientid drugcode prescriptiondate  indexdate ## 1         a1        3       2014-08-07 2014-09-30 ## 2         a1        3       2014-09-08 2014-09-30 ## 3         a1        7       2014-09-19 2014-09-30 ## 4         a1        5       2014-09-30 2014-09-30 ## 5         a2        4       2014-07-11 2014-08-26 ## 6         a2        4       2014-07-21 2014-08-26 ## 7         a2        3       2014-08-13 2014-08-26 ## 8         a2        5       2014-08-26 2014-08-26 ## 9         a2        5       2014-09-30 2014-08-26 ## 10        a3        2       2014-08-16       <na> ## 11        a3        3       2014-09-17       <na> ## 12        a4        5       2014-06-08 2014-10-22 ## 13        a4        5       2014-06-29 2014-10-22 ## 14        a4        6       2014-08-20 2014-10-22 ## 15        a4        6       2014-09-24 2014-10-22 ## 16        a4        4       2014-10-22 2014-10-22 ## 17        a4        4       2014-10-25 2014-10-22 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -