首页 > 解决方案 > 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))

标签: r

解决方案


请注意,这两个变量是因素,而不是日期(甚至是字符/字符串)。将它们转换为字符串,然后转换为日期时间。

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(). 理想情况下,这些错误消息也表示为文本,而不是屏幕截图。


推荐阅读