r - 将时间和日期分成不同的列(格式=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”等等……
解决方案
提取 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)
推荐阅读
- python - 白色背景的黑白图
- javascript - 给定日期间隔范围,创建一个包含一天中所有 1440 分钟的数组,其中每分钟出现在源间隔中的频率
- r - 停止 R 中的自动扩展功能
- javascript - 如何在反应中使用带有 sass 模块的 SASS 父选择器
- python - Python中关于if语句和空字符串的问题
- javascript - 实现 js 类属性的钩子
- rust - 移动到过滤器闭包中的特征参数的生命周期限制应该是什么?
- javascript - ReactJS 返回待处理的承诺
- angular - 嵌套表单数组问题:错误错误:找不到带有路径的控件:'问题 - > 0 - > 代码'
- c - 双向通信管道