r - R studio 时差 mm/dd/yy hh:mm 格式
问题描述
考虑到 POSTX 格式等,我尝试了许多命令来查找所有相关命令的持续时间。但是 Rstudio 继续输出错误消息。这些示例表明“-”有效。例如,我在数据框中有两列。
并尝试使用诸如。 解析时间的代码和输出图像
> dput(file1[, c("V8", "V9")])
structure(list(V8 = structure(1:19, .Label = c("4/6/2018 14:39",
"4/6/2018 15:04", "4/6/2018 15:09", "4/6/2018 15:28", "4/6/2018 15:56",
"4/6/2018 16:02", "4/6/2018 16:07", "4/6/2018 16:10", "4/6/2018 16:11",
"4/6/2018 16:27", "4/6/2018 16:41", "4/6/2018 16:43", "4/6/2018 16:57",
"4/6/2018 17:13", "4/6/2018 17:17", "4/6/2018 17:30", "4/6/2018 17:38",
"4/6/2018 17:47", "4/6/2018 17:52"), class = "factor"), V9 = structure(c(1L,
3L, 2L, 4L, 8L, 5L, 6L, 9L, 7L, 12L, 19L, 16L, 11L, 13L, 10L,
14L, 17L, 18L, 15L), .Label = c("4/6/2018 15:00", "4/6/2018 15:26",
"4/6/2018 15:32", "4/6/2018 16:01", "4/6/2018 16:10", "4/6/2018 16:12",
"4/6/2018 16:18", "4/6/2018 16:35", "4/6/2018 16:46", "4/6/2018 17:24",
"4/6/2018 17:37", "4/6/2018 17:38", "4/6/2018 17:46", "4/6/2018 18:24",
"4/6/2018 18:46", "4/6/2018 19:21", "4/6/2018 20:14", "4/6/2018 20:35",
"4/6/2018 21:44"), class = "factor")), .Names = c("V8", "V9"), class = "data.frame", row.names = c(NA,
-19L))
解决方案
请注意,这两个变量是因素,而不是日期(甚至是字符/字符串)。将它们转换为字符串,然后转换为日期时间。
ds$V8 <- strptime(as.character(ds$V8), format="%m/%d/%Y %H:%M")
ds$V9 <- strptime(as.character(ds$V9), format="%m/%d/%Y %H:%M")
difftime(ds$V9, ds$V8, units="secs")
结果:
Time differences in secs
[1] 1260 1680 1020 1980 2340 480 300 2160 420 4260 18180 9480
[13] 2400 1980 420 3240 9360 10080 3240
请注意,感谢您遵循 @Maurits Evers 和 @Rui Barradas 的建议并使用dput()
. 理想情况下,这些错误消息也表示为文本,而不是屏幕截图。
推荐阅读
- javascript - JS 无法加载 web worker 模块,除非 worker.js 在 public/
- javascript - 为什么使用对数会给出 8e+2 的值?
- python - 在 python 中,当我不知道不需要的数据将在哪里弹出或特定字符串是什么时,从数据列表中删除不需要的项目?
- java - 正则表达式:解析包含最后一个、第一个和中间初始值的字符串
- javascript - 对象reactjs内的数组上的onChange
- node.js - NOT_FOUND:从 Firebase 函数调用自定义 Google Cloud ML 模型时资源 ID 无效
- string - 如何使用 shell 脚本将文件名替换为该文件的内容
- typescript - 如何获取对象的形状然后修改每个叶子属性的类型?
- python - python嵌套循环“if”语句
- php - 如何将外键值插入表中?