Another option to achive this, without the need to set options(digits.secs=3)
is using format()
:
format(Sys.time(), "%Y-%m-%d %H:%M:%OS3")
# alternative:
format(Sys.time(), digits = 3L)
time <- "2010-01-15 13:55:23.975"
# using strptime to convert the string
format(strptime(time, "%Y-%m-%d %H:%M:%OS"), "%Y-%m-%d %H:%M:%OS3")
format(strptime(time, "%Y-%m-%d %H:%M:%OS"), digits = 3L)
# using as.POSIXct to convert the string (under the hood also using strptime())
format(as.POSIXct(time), "%Y-%m-%d %H:%M:%OS3")
format(as.POSIXct(time), digits = 3L)
However, regarding as.POSIXct
please see this.