我将以下日期格式存储为数据框中其中一列的因子:
2017-02-19T21:10:18.664+00:00
2017-03-10T21:40:50.398+00:00
示例数据:
head(dataset$local_end_datetime)
[1] 2017-02-19T21:10:18.664+00:00 2017-02-19T21:10:38.418+00:00
[3] 2017-03-10T21:40:50.398+00:00 2017-03-11T16:41:43.339+00:00
[5] 2017-03-10T21:43:31.092+00:00 2017-03-10T21:34:36.065+00:00
我需要将日期和时间分开。我可以找到使用下面提取的日期:
dataset$Date <- as.Date(dataset$local_end_datetime)
如何从上面的格式中提取时间?我不知道正则表达式。
答案 0 :(得分:1)
Library Lubridate is quite powerful when it comes to manipulating dates
dataset$datetime <- lubridate::ymd_hms(dataset$local_end_datetime)
dataset$Local_Date <- as.Date(dataset$datetime)
dataset$Local_Time <- format(dataset$datetime,"%H:%M:%S")