我有以下包含时间戳的矢量。这些时间戳被记录为因素。
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我该如何解决这个问题?
发布于 2019-02-13 20:53:31
您必须提供秒:
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:00https://stackoverflow.com/questions/54679221
复制相似问题