I am trying to correct and reformat Timestamps in a 24 hour format, however they are missing enough numbers to use the POSIXct function correctly.
Here is an example of the Date and Timestamps. Within the data frame their is a column for Year (ex:2018), DOY-day of year (ex:305), and TIME (ex: 0).
The TIME column is giving me issues. The values look like (0, 30, 100, 130, 200). In this case 0 should be 00:00:00 if formatted to a %H:%M:%S format. My issue is I need to figure out how to format it that way.
Here is the code I tried:
mt$date <- as.POSIXct(paste(mt$Year, mt$DOY, mt$Time), format = "%Y %j %H")
Thanks for any help, I am sure this is likely an easy fix.
We could make use of sprintf
to create the format
mt$date <- with(mt, as.POSIXct(paste0(Year, '-', DOY,' ',
sub('^(..)', '\\1:', sprintf('%04d', Time))), format = "%Y-%j %H:%M"))
mt$date
#[1] "2018-11-01 00:00:00 EDT" "2018-11-08 00:30:00 EST" "2018-06-24 01:00:00 EDT"
#[4] "2018-08-13 01:30:00 EDT" "2018-11-16 10:00:00 EST"
mt <- structure(list(Year = c(2018, 2018, 2018, 2018, 2018), DOY = c(305,
312, 175, 225, 320), Time = c(0, 30, 100, 130, 1000)),
class = "data.frame", row.names = c(NA,
-5L))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments