使用Chron转换时间无法正常工作

时间:2019-02-13 20:51:43

标签: r chron

我有以下包含时间戳的向量。这些时间戳记为因素。

times<-as.factor(c("8:24", "9:17","11:52","1:49","9:36"))

我想使用chron包将它们转换为时间对象。而且我使用了以下代码

library(chron)

chron(as.character(times), format = "h:m")

但是,当我运行它时,状态会显示

Error in widths[, fmt$periods, drop = FALSE] : subscript out of bounds

如何解决这个问题

1 个答案:

答案 0 :(得分:0)

您必须提供第二名:

times(paste0(times, ":00"))
## [1] 08:24:00 09:17:00 11:52:00 01:49:00 09:36:00

这些工作也可以:

times(c(as.matrix(read.table(text = as.character(times), sep = ":")) %*% c(1, 1/60)/24))
## [1] 08:24:00 09:17:00 11:52:00 01:49:00 09:36:00

times(as.numeric(sub(":.*", "", times)) / 24 + as.numeric(sub(".*:", "", times)) / (24 * 60))
## [1] 08:24:00 09:17:00 11:52:00 01:49:00 09:36:00