R Studio时差,单位为mm / dd / yy hh:mm格式

时间:2018-06-21 03:21:47

标签: r

考虑到POSTX格式,我尝试了许多命令从所有相关命令中找到持续时间,但是Rstudio继续输出错误消息。这些示例指出“-”有效。 例如,我在数据框中有两列。

并尝试使用诸如的命令查找持续时间。 image of code and output from parsed time

并根据原始数据帧进行计算 image of code and output from original data frame

> 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))

1 个答案:

答案 0 :(得分:2)

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

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