R POSIXct date difference produce artificial decimal places

46 Views Asked by At

I have a POSIXct date column, and try to baseline it. However, I found the difftime() (or just -) operation produce artificial decimal places that should never exist.

>center_coor[,dateTime]
[1] "2023-06-12 12:44:00.375 BST" "2023-06-12 12:44:00.390 BST" "2023-06-12 12:44:00.417 BST"

>center_coor[,difftime(dateTime,dateTime[1])]
Time differences in secs
[1]  0.00000000  0.01500010  0.04299998

I definitely don't want those artificial decimal places. How could this happen? and how can I disable them in difftime() other than using round() afterwards?

Updated 1 using dput() really make no change to the data and just give all the artificial decimal places.

>center_coor[,dateTime]]%>%dput()
structure(c(1686570240.375, 1686570240.39, 1686570240.418), class = c("POSIXct", "POSIXt"), tzone = "GB")

>center_coor[,difftime(dateTime,dateTime[1])]]%>%dput()
structure(c(0, 0.0150001049041748, 0.0429999828338623), class = "difftime", units = "secs")

For a reproducible example, you can most certainly copy and convert the characters provided into a POSIXct column llike below, and then take the difference. Again, it produce artificial decimal places.

>temp=data.table(dateTime=c("2023-06-12 12:44:00.375 BST", "2023-06-12 12:44:00.390 BST", "2023-06-12 12:44:00.417 BST")%>%as.POSIXct(Time,format="%Y-%m-%d %H:%M:%OS", tz='GB'))
>temp[,difftime(dateTime,dateTime[1])]%>%dput()
structure(c(0, 0.0150001049041748, 0.0420000553131104), class = "difftime", units = "secs")
0

There are 0 best solutions below