as.ITime error"无效的子字符串参数"

时间:2017-09-17 00:20:25

标签: r function time data.table

该函数给出了意想不到的结果,并显示错误消息。 看看我发现了什么

as.ITime(reff[["Saida"]])
 [1] "05:24:00"


as.ITime(df[,"Saida"])
 [1] "04:30:00"

之后,我执行:

diff(reff[["Saida"]], df[,"Saida"])
Error in substring(paste("0", hh, sep = ""), nchar(paste(hh))) : 
invalid substring arguments

我该如何解决这个问题?有什么想法出现这个错误?

PS:

这是df

structure(list(Linha = 307L, Sentido = 1L, Saida = structure(16200L, class = "ITime"), 
Chegada = structure(16740L, class = "ITime"), Catraca = 0L, 
Embarcado = 25L, dia = "sexta", feriado = TRUE, isOL = FALSE, 
antes = TRUE, proximo = TRUE), .Names = c("Linha", "Sentido", 
"Saida", "Chegada", "Catraca", "Embarcado", "dia", "feriado", 
"isOL", "antes", "proximo"), row.names = 747092L, class = "data.frame")

这是reff

structure(list(Linha = 307L, Sentido = 1L, Saida = structure(19440L, class = "ITime"), 
Chegada = structure(20700L, class = "ITime"), Catraca = 35L, 
Embarcado = 125L, dia = "sexta", feriado = TRUE, isOL = TRUE, 
antes = TRUE, proximo = TRUE), .Names = c("Linha", "Sentido", 
"Saida", "Chegada", "Catraca", "Embarcado", "dia", "feriado", 
"isOL", "antes", "proximo"), row.names = 747094L, class = "data.frame")

1 个答案:

答案 0 :(得分:2)

您需要difftime(请参阅?difftime)而不是diff(请参阅?diff):

difftime(reff[["Saida"]], df[,"Saida"])
  

时差54分钟

diff用于创建1个变量的滞后。 difftime用于区分2个时间/日期变量。