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.
format()
library(dplyr) library(tidyr) library(lubridate) 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")) options(digits.secs=3) df %>% separate(timestamp, c('index', 'datetime'), sep = ' ') %>% mutate(time_clean = lubridate::ymd_hms(datetime)) %>% mutate(date = format(time_clean,format = '%F'), time = format(time_clean,format = '%H:%M:%OS')) %>% select(date, time) #> date time #> 1 2018-11-08 07:41:55.921 #> 2 2018-11-08 07:42:29.842 #> 3 2018-11-08 07:42:29.845 #> 4 2018-11-08 07:42:29.845 #> 5 2018-11-08 07:43:13.957
Created on 2019-01-26 by the reprex package (v0.2.1)
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.
I am getting this message when i try to convert the whole dataframe:
Warning message: Expected 2 pieces. Missing pieces filled with NA in 1543681 rows [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...]
NA
No need for convert it back, the POSIXct object is already stored with full precision, you are just not seen it because of your digits.secs option.
digits.secs
library(dplyr) library(tidyr) library(lubridate) 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")) options(digits.secs=3) # Change digits.sec to show milliseconds df %>% separate(timestamp, c('index', 'datetime'), sep = ' ') %>% mutate(time_clean = lubridate::ymd_hms(datetime)) #> index datetime time_clean #> 1 1 2018-11-08T07:41:55.921Z 2018-11-08 07:41:55.921 #> 2 2 2018-11-08T07:42:29.842Z 2018-11-08 07:42:29.842 #> 3 3 2018-11-08T07:42:29.845Z 2018-11-08 07:42:29.845 #> 4 4 2018-11-08T07:42:29.845Z 2018-11-08 07:42:29.845 #> 5 5 2018-11-08T07:43:13.957Z 2018-11-08 07:43:13.957
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.
Prefect. Thanks Very Much for your help. That's very helpful.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.