2

I am doing some animal movement analysis and I want to submit data to an organisation called Movebank for annotation, but they require the timestamp to have milliseconds included with 3 decimal places.

I have a column in my data frame (dat) with my timestamps as characters (without milliseconds), for example "2017-07-19 16:30:24"

To convert them to time and date format with milliseconds I am using the code:

options(digits.secs = 3)
dat$timestamp <- as.POSIXct(dat$timestamp, format = "%Y-%m-%d %H:%M:%OS", tz = "UTC")

Which works fine at converting my timestamp column to POSIXct which I can use to make tracks etc., but it does not add .000 milliseconds to the end of each timestamp which I was hoping it would.

I have also tried:

dat$timestamp <- as.POSIXct(dat$timestamp, format = "%Y-%m-%d %H:%M:%OS3", tz = "UTC")

(Note: I added .. %OS3 ...) But this returns an NA for my for my timestamps.

Can anybody shed some light on this? I essentially need to add .000 to the end of each of my timestamps so that, using the example given above, I would have the format "2017-07-19 16:30:24.000"

jmccucc
  • 23
  • 3

1 Answers1

3

The milliseconds will be dropped if there are no times with effective milliseconds.

options(digits.secs=4)

x1 <- as.POSIXct("2017-07-19 16:30:25")
as.POSIXct(paste0(x1, ".000"), format="%Y-%m-%d %H:%M:%OS")
# [1] "2017-07-19 16:30:25 UTC"

However, they will be added automatically if there are.

x2 <- as.POSIXct("2017-07-19 16:30:25.002")
c(x1, x2)
# [1] "2017-07-19 18:30:25.000 CEST" "2017-07-19 18:30:25.002 CEST"
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Yeah I think that worked. I used: ```dat$timestamp <- paste0(dat$timestamp, ".001")```, then continued with the code I have above. – jmccucc Aug 27 '20 at 13:30
  • @jmccucc Did you get `"2017-07-19 16:30:25.001"` or ``"2017-07-19 16:30:25.000"`` with that? – jay.sf Aug 27 '20 at 13:40
  • I got ```"2017-07-19 16:30:25.000"``` strangely enough, I didn't question it though because it's what I wanted anyway. Any idea why that would happen? – jmccucc Aug 28 '20 at 10:18
  • @jmccucc I'm not exactly sure, but [this answer](https://stackoverflow.com/a/10700025/6574038) sheds some light on it. Accordingly it's probably better to use `strptime(paste0(x1, ".001"), format="%Y-%m-%d %H:%M:%OS")`. – jay.sf Aug 28 '20 at 10:32