首页 > 解决方案 > 将时间和日期分成不同的列(格式=2019-05-26T13:50:56.335288Z)

问题描述

在我的数据中,时间和日期存储在“cord”列中(class=factor)。我想将日期和时间分成两个单独的列。

数据如下所示:

1 2019-05-26T13:50:56.335288Z
2 2019-05-26T17:55:45.348073Z
3 2019-05-26T18:12:00.882572Z 
4 2019-05-26T18:26:49.577310Z 

我已经使用以下方法成功提取了日期:cord$Date <- as.POSIXct(cord$Time)

但是,我无法找到以“H:M:S”格式提取时间的方法。

的输出dput(head(cord$Time))返回一长串时间戳:“2020-04-02T13:34:07.746777Z”、“2020-04-02T13:41:11.095014Z”、“2020-04-02T14:08:05.508818Z”、“ 2020-04-02T14:17:10.337101Z”等等……

标签: rdatetimesplittelemetry

解决方案


提取 H:M:S

library(lubridate)
format(as_datetime(cord$Time), "%H:%M:%S")
#> [1] "13:50:56" "17:55:45" "18:12:00" "18:26:49"

如果您也需要毫秒:

format(as_datetime(cord$Time), "%H:%M:%OS6")
#> [1] "13:50:56.335288" "17:55:45.348073" "18:12:00.882572" "18:26:49.577310"

绳子在哪里:

cord <- read.table(text = "  Time
1 2019-05-26T13:50:56.335288Z
2 2019-05-26T17:55:45.348073Z
3 2019-05-26T18:12:00.882572Z 
4 2019-05-26T18:26:49.577310Z ", header = TRUE)

推荐阅读