Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge nearest date, and related variables from a another dataframe by group

Tags:

r

I have two dataframes each with multiple rows per ID. I need to return the closest date and related data from the second dataframe based on the ID and date of the first dataframe - adding the related data to the first dataframe. This also has to work with NAs present in the second dataframe. Example data:

set.seed(42)
df1 <- data.frame(ID = sample(1:3, 10, rep=T), dateTarget=(strptime((paste
    (sprintf("%02d", sample(1:30,10, rep=T)), sprintf("%02d", sample(1:12,10, rep=T)), 
     (sprintf("%02d", sample(2013:2015,10, rep=T))), sep="")),"%d%m%Y")), Value=sample(15:100, 10, rep=T))
df2 <- data.frame(ID = sample(1:3, 10, rep=T), dateTarget=(strptime((paste
     (sprintf("%02d", sample(1:30,20, rep=T)), sprintf("%02d", sample(1:12,20, rep=T)), 
     (sprintf("%02d", sample(2013:2015,20, rep=T))), sep="")),"%d%m%Y")), ValueMatch=sample(15:100, 20, rep=T))

Something from base preferable - split and a mixture of the apply family?

The final table would look something like:

  ID dateTarget Value dateMatch ValueMatch
1  3   22-02-15    52  09-03-15         94
2  1   29-12-14    18  06-12-14         88
3  3   08-12-15    98  06-07-15         48
4  2   14-01-13    52  08-04-13         77
5  2   29-07-15    97  01-08-15         68
6  3   30-05-13    91  01-04-13         85
7  1   04-11-13    70  21-02-14         35
8  2   15-06-15    98  01-08-15         68
9  3   17-11-14    68  15-12-14         95

P.S. Are there better ways of generating random dates (not using seq.Date)?

like image 275
Simon Avatar asked Jan 21 '15 16:01

Simon


2 Answers

Here is the solution based on the base package:

z <- lapply(intersect(df1$ID,df2$ID),function(id) {
   d1 <- subset(df1,ID==id)
   d2 <- subset(df2,ID==id)

   d1$indices <- sapply(d1$dateTarget,function(d) which.min(abs(d2$dateTarget - d)))
   d2$indices <- 1:nrow(d2)

   merge(d1,d2,by=c('ID','indices'))
  })

z2 <- do.call(rbind,z)
z2$indices <- NULL

print(z2)

#    ID dateTarget.x Value dateTarget.y ValueMatch
# 1   3   2015-11-14    47   2015-07-06         48
# 2   3   2015-12-08    98   2015-07-06         48
# 3   3   2015-02-22    52   2015-03-09         94
# 4   3   2014-11-17    68   2014-12-15         95
# 5   3   2013-05-30    91   2013-04-01         85
# 6   1   2013-11-04    70   2014-02-21         35
# 7   1   2014-12-29    18   2014-12-06         88
# 8   2   2013-01-14    52   2013-04-08         77
# 9   2   2015-07-29    97   2015-08-01         68
# 10  2   2015-06-15    98   2015-08-01         68
like image 185
Marat Talipov Avatar answered Sep 22 '22 16:09

Marat Talipov


We can also do this by one-liner with dplyr.

library(dplyr)

left_join(df1, df2, by = "ID") %>%
  mutate(dateDiff = abs(dateTarget.x - dateTarget.y)) %>%
  group_by(ID, dateTarget.x) %>%
  filter(dateDiff == min(dateDiff))
like image 42
teru Avatar answered Sep 22 '22 16:09

teru