I would like to convert timestamp by milliseconds. Can i still do this with the lubridate package?
library(lubridate)
library(tidyr)
library(dplyr)
df <- data.frame(timestamp = c("1 2018-11-08T07:41:55.921Z",
"2 2018-11-08T07:42:29.842Z",
"3 2018-11-08T07:42:29.845Z",
"4 2018-11-08T07:42:29.845Z",
"5 2018-11-08T07:43:13.957Z"))
df %>%
#note your timestamp has index info, separating that out
separate(timestamp, c('index', 'datetime'), sep = ' ') %>%
# converting the form of "2018-11-08T07:41:55.921Z" to POSIXct date-time objects
# check out `?ymd_hms` docs if you want to force a timezone other than UTC
mutate(time_clean = lubridate::ymd_hms(datetime)) %>%
mutate(
# see https://stat.ethz.ch/R-manual/R-devel/library/base/html/strptime.html
# formating details
date = format(time_clean,format = '%F'),
time = format(time_clean,format = '%T')
)
You are not using lubridate for printing the timestamp to any particular format, that is done by base R format() function, and yes, you can get miliseconds too. Just have in mind that the result is a character vector and you are not going to be able to directly perform calculations with it or use it on an axis with a continuous scale.
Many Thanks. Can I convert it back to time format to perform any calculation? The reason I want to change it to show in milliseconds so, I can merge the data with the timestamp to another data frame. Merging to the nearest minutes, I missing a lot of values.
About your warning message, I can't tell why is happening because I don't have access to your data, but is very likely that you have some missing values.