I have this sample dataframe:
df <- data.frame(ID = c("5","5","5","5","5","5" ,"5" ,"5","5","5","5","14","14","14","14" ,"14","14"),
Date1= c("22/07/2014","22/07/2014","22/07/2014"
,"22/07/2014"
,"22/07/2014"
,"22/07/2014"
,"22/07/2014"
,"22/07/2014"
,"22/07/2014"
,"22/07/2014"
,"22/07/2014"
,"08/11/2016"
, "08/11/2016"
, "08/11/2016"
, "08/11/2016"
, "08/11/2016"
, "08/11/2016"),
Date2= c("01/01/2011"
,"01/08/2011"
,"01/12/2010"
,"10/11/2015"
,"22/07/2014"
,"01/01/2013"
,"23/04/2014"
,"01/01/2006"
,"01/01/2013"
,"01/10/2012"
,"01/08/2012"
,"14/04/2015"
,"01/10/2008"
,"01/10/2008"
,"14/05/2015"
,"11/04/2015"
,"05/10/2008"),
stringsAsFactors = F)
where I have each ID repeated several times. I need to get a dataframe with only 1 line per ID. As you can see, each patient has only one date in column df$date1, so the condition to select 1 row per patient would be: to pick the closest date between date 1 and date 2.
How could I do that?
Thansk