Reputation: 518
I have two data.tables I'm trying to merge via a date column, now my first data.table's date column is of class "POSIXct" "POSIXt" and has this format:
> DT1[,.(study_date)]
study_date
1: 2008-02-13 08:44:01
2: 2009-03-22 10:18:58
3: 2009-03-27 12:01:28
4: 2009-04-21 09:02:33
5: 2009-11-02 08:38:52
---
230656: 2009-05-26 15:42:58
230657: 2011-03-29 17:05:06
230658: 2012-01-20 15:28:53
230659: 2016-02-17 14:19:14
230660: 2012-11-23 07:10:09
> DT1[,class(study_date)]
[1] "POSIXct" "POSIXt"
My second data.table's date column is of class character and has this format:
> DT2[,.(study_date)]
study_date
1: 13FEB2008:08:44:01
2: 22MAR2009:10:18:58
3: 22MAR2009:10:18:58
4: 27MAR2009:12:01:28
5: 27MAR2009:12:01:28
---
298011: 29MAR2011:17:05:06
298012: 20JAN2012:15:28:53
298013: 17FEB2016:14:19:14
298014: 23NOV2012:07:10:09
298015: 23NOV2012:07:10:09
I ideally wanted to used lubridate to change the date class and format of DT2 to match the format and the class of the dates in DT1.
Which way would be the best way to convert the dates in DT2 so they match the format and class of the dates in DT1?
Upvotes: 1
Views: 859
Reputation: 887128
With lubridate
, it can be dmy_hms
library(data.table)
library(lubridate)
DT2[, study_date := dmy_hms(study_date)]
Or using as.POSIXct
DT2[, study_date := as.POSIXct(study_date, format = '%d%b%Y:%H:%M:%S')]
Upvotes: 2