R的天数时差

时间:2016-05-22 19:45:11

标签: r date

我无法在两个日期之间找到时差。这是数据样本:

          Id       second_date     first_date
1      5402v        5-Dec-2014     9-May-1968
2     a3169y        7-Jul-2014    18-May-1936
3     a6230g        9-Oct-2015    30-Jun-1961
4     a6991u       25-Nov-2014     3-May-1982
5     a7481m        6-Apr-2014    29-Oct-1941
6     a8749y       28-Jan-2013    13-Jan-1977

我想减去两个日期以获得天数,但由于某种原因,我会得到NA。我尝试了两种方法:

diff<-merge(Table2[!duplicated(Table2$Id),], Table4, by = "Id")
diff$second_date<-as.POSIXct(diff$second_date, format="%d/%m/%Y")
diff$first_date<-as.POSIXct(diff$first_date, format="%d/%m/%Y")
diff$diff<-diff$second_date - diff$first_date

diff$diff<- difftime(diff$second_date, diff$first_date, units = c("days"))

0 个答案:

没有答案