Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parsing and working with microsecond-precision timestamps in R using dplyr

I’m planning to collect data with microsecond-precision timestamps in the format "DD/MM/YY HH:MM:SS.mmmuuun"? How would I parse in R and keep working in a dplyr pipeline?

Example:

"26/09/24 11a:16:36.5933210"
df <- data.frame(
  ID = c(1, 1, 1),
  Timestamp = c("26/09/24 11a:16:36.5933210",
                "26/09/24 11a:16:36.6518648",
                "26/09/24 11p:16:36.7280308"),
  CH = c(11, 11, 11)
)

I would still like to retain the date and time information for later correlation with observations. Note "a" for am and "p" for pm.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Using dmy_hms with AM/PM moved to the end of the string.

library(dplyr)
library(lubridate)

df %>% 
  mutate(Timestamp_new = dmy_hms(sub("( \\d{2})([ap])(.*$)", "\\1\\3 \\2m", Timestamp)),
         ms = format(Timestamp_new, "%OS3"))
  ID                  Timestamp CH       Timestamp_new     ms
1  1 26/09/24 11a:16:36.5933210 11 2024-09-26 11:16:36 36.593
2  1 26/09/24 11a:16:36.6518648 11 2024-09-26 11:16:36 36.651
3  1 26/09/24 11a:16:36.7280308 11 2024-09-26 11:16:36 36.728

Note you can also use as.POSIXct with "%d/%m/%y %I:%M:%OS3"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading