查找R中两个数据集的最接近时间

时间:2017-02-02 02:20:10

标签: r dataframe time comparison

我有两个数据集:

 patient<-c("A","A","A","B","B")
 time<-c("24Nov2016 14:05:00","25Nov2016 14:51:00","26Nov2016 21:57:00","24Nov2016 00:55:00","24Nov2016 11:55:00")
 DS1<-data.frame(patient,time)

另一个数据集:

 patient2<-c("A","A","A","A","B","B","B")
 time2<-c("24Nov2016 14:06:00","25Nov2016 14:52:00","26Nov2016 15:57:00","26Nov2016 21:57:00","24Nov2016 00:54:00","24Nov2016 03:59:00","24Nov2016 11:59:00")
 DS2<-data.frame(patient2,time2)

对于第一个数据集(DS1)中的每一行,如果患者在DS1和DS2中相同,我想找到最接近的时间2

结果:

   patient     time                closestTime
      A    24Nov2016 14:05:00      24Nov2016 14:06:00
      A    25Nov2016 14:51:00      25Nov2016 14:52:00
      A    26Nov2016 21:57:00      26Nov2016 21:57:00
      B    24Nov2016 00:55:00      24Nov2016 00:54:00
      B    24Nov2016 11:55:00      24Nov2016 11:59:00

我是R的新手,所以任何帮助都会受到赞赏。另外,我不确定是否需要首先更改时间数据字段的数据类型。

0 个答案:

没有答案