简体   繁体   中英

Combine millisecond values and date to create POSIX friendly format in R

My problem is that I have to combine two strings, one initially formatted as %H,%M,%S, and another that represents ms

df = data.frame(hms = "14:39:12", ms = seq(999, 1005, by = 0.65))

How can I combine and convert this to represent a standard time format with hours, minutes, seconds, milliseconds, by having the ms roll over into S and avoiding the issue of having instances where my ms is greater than 1000ms

Thanks for your help

Solved turning it into a character string that represents the desired output.

# Data frame
df = data.frame(hms = "14:39:12", ms = seq(999, 1005, by = 0.65)) %>%
        tidyr::separate(., col = hms, sep = ":", into = c("H", "M", "S")) %>%
        mutate_all(as.numeric) %>%
        mutate(S = ifelse(ms >= 1000, S+1, S)) %>%
        mutate(ms = ifelse(ms >= 1000, ms-1000, ms))

# Round values to include two points after decimal
df$ms = round(ret$ms, digits = 5)

# Recombine
df = paste(ret$H, ret$M, ret$S, ret$ms, sep = ":")

While this formats the data the way I need, I still need to check if R will recognize it as a date and if it can be used as discrete values while plotting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM