Reputation: 23
Im trying to calculate the difftime in minutes between two columns and the results seems to be not right.
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 |
the code to calculate diff is the following:
test$diff <- difftime(test$a1_0,test$BLOCK_DATE_TIME.x,units = "mins")
The desire outcome is for the first line 833 and for the second 30.
Thanks in advance
Upvotes: 1
Views: 299
Reputation: 887028
It is possible that the columns are not Datetime class. If we convert to POSIXct
, it works as expected
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")
Upvotes: 1