diff time r studio 没有正确给出结果

diff time r studio is not giving the outcome properly

我正在尝试计算两列之间以分钟为单位的差异时间,但结果似乎不正确。

BLOCK_DATE_TIME.x a1_0 diff
2019-04-26 19:07:00 2019-04-27 09:00:00 773
2019-08-27 08:30:00 2019-08-27 09:00:00 -30

计算diff的代码如下:

test$diff <- difftime(test$a1_0,test$BLOCK_DATE_TIME.x,units = "mins")

想要的结果是第一行833,第二行30。

提前致谢

列可能不是日期时间class。如果我们转换为 POSIXct,它会按预期工作

library(dplyr)
library(lubridate)
test1 <- test %>% 
      mutate(across(c(BLOCK_DATE_TIME.x, a1_0), ymd_hms),
         diff = difftime(a1_0, BLOCK_DATE_TIME.x, units = "mins"))
test1
#    BLOCK_DATE_TIME.x                a1_0     diff
#1 2019-04-26 19:07:00 2019-04-27 09:00:00 833 mins
#2 2019-08-27 08:30:00 2019-08-27 09:00:00  30 mins

数据

test <- structure(list(BLOCK_DATE_TIME.x = c("2019-04-26 19:07:00", 
"2019-08-27 08:30:00"
), a1_0 = c("2019-04-27 09:00:00", "2019-08-27 09:00:00")), row.names = c(NA, 
-2L), class = "data.frame")