Converting numbers into time and date

I am trying to convert numeric values into times and dates. I am working with a data set so it would be appreciated if you should show an example using a dataset.

Here are some examples, converting 93537 into 09:35:57 (HH:MM:SS). Additionally, I need to convert 220703 into 22-07-03 (YY:MM:DD).

I will add an example of my code below:

CPLF_data$HMS <- substr(as.POSIXct(sprintf("%04.0f", CPLF_data$StartTime), format='%H%M%S'), 12, 16)

CPLF_data$YMD <- as.POSIXct(CPLF_data$Date, tz="UTC", origin ="1970-01-01", format ="%Y-%M-%D")

The first line is correct however, it does not show seconds. The second line is incorrect.

Thank you.

an example of what I want my data to look like, this is not perfect as I am still having trouble with the code

I want my final product to be a new column with the times and dates in the correct format with their own columns.

Upvotes: 1

Views: 406

Answers (3)

Andrew15
Andrew15

Reputation: 103

Try the ymd_hms function in the lubridate package.

output$datetime <- ymd_hms(paste(input$year, input$month, input$day,
                                 input$HH, input$MM, input$SS, sep="-"))

You can enter 00 if you don't have seconds, for example ....

Upvotes: 2

G. Grothendieck
G. Grothendieck

Reputation: 270248

Use chron times class to get the times or if a character string is wanted use as.character on that. Use as.Date to get a Date class object. The sub puts colons between the parts of the time after which we can convert it to times class. The sprintf pads the date with 0 on the left if it is only 5 characters and otherwise leaves it as 6 characters and then we convert that to Date class.

library(chron)
time <- 93537 
date <- 220703 

tt <- times(sub("(..)(..)$", ":\\1:\\2", time))
tt
## [1] "09:35:37"

as.character(tt)
## [1] "09:35:37"    

dd <- as.Date(sprintf("%06d", date), "%y%m%d")
dd
## [1] "2022-07-03"

as.character(dd)
## [1] "2022-07-03"

Upvotes: 2

r2evans
r2evans

Reputation: 160952

Base R does not have a class for just "time" (of day), as.POSIXct doesn't deal with "times", it deals with "date-times". The lubridate:: package does give number-like HMS values, which may be relevant, but since each row has both date and time, it seems relevant to combine them instead of putting them into separate columns.

CPLF_data |>
  transform(
    StartTime = as.numeric(StartTime),
    Date = as.numeric(Date)
  ) |>
  transform(
    DateTime = ISOdate(
      2000 + Date %/% 10000, (Date %% 10000) %/% 100, Date %% 100,
      StartTime %/% 10000, (StartTime %% 10000) %/% 100, StartTime %% 100)
  )
#   StartTime   Date            DateTime
# 1     93537 220703 2022-07-03 09:35:37

Note: I'm assuming that all years are 2-digits and at/after 2000. If this is not true, it's not difficult to work around it with some custom code. Also, over to you if you want to set the timezone of this timestamp by adding tz="US/Mountain" or whichever is more appropriate for the data.


Data

CPLF_data <- data.frame(StartTime = "93537", Date = "220703")

Upvotes: 0

Related Questions