R - Converting Date and Time Fields to Posixct with Hhmmss Format

R - converting date and time fields to POSIXct with HHMMSS format

You were very close. The following "simply" forces the first two columns to be read as character strings, which saves the leading zeros.

R> df <- read.table(text="20010101 000000  0.833
20010101 000500 0.814
20010101 001000 0.794
20010101 001500 0.772",
+ header=FALSE, colClasses=c("character", "character", "numeric"),
+ col.names=c("Date", "Time", "Val"))
R> df
Date Time Val
1 20010101 000000 0.833
2 20010101 000500 0.814
3 20010101 001000 0.794
4 20010101 001500 0.772

Now what you were attempting "just works":

R> df$DateTime <- as.POSIXct(paste(df$Date, df$Time), format="%Y%m%d %H%M%S")
R> df
Date Time Val DateTime
1 20010101 000000 0.833 2001-01-01 00:00:00
2 20010101 000500 0.814 2001-01-01 00:05:00
3 20010101 001000 0.794 2001-01-01 00:10:00
4 20010101 001500 0.772 2001-01-01 00:15:00
R>

Formatting a duration as HH:mm:ss in R

And another answer using lubridate and hms:

library(lubridate)
library(hms)

df <- data.frame(Duration = c(19, 250, 3, 3600, 86401))
df$Expected <- hms::hms(seconds_to_period(df$Duration))

df
Duration Expected
1 19 00:00:19
2 250 00:04:10
3 3 00:00:03
4 3600 01:00:00
5 86401 24:00:01

R: Converting date/time format 0000-00-00T00:00:00Z to POSIXct

We can use ymd_hms from lubridate

df1$Timestamp <- lubridate::ymd_hms(df1$Timestamp)

R: convert Date/Time columns to POSIXct

paste variables into one and then convert to POSIX.

as.POSIXct(paste(15,     6,    12,    16,     0,    10), format = "%y %m %d %H %M %S")

change paste to your columns:

as.POSIXct(paste(df$YY, df$MM, df$DD, df$hh, df$mm, df$ss), format = "%y %m %d %H %M %S")

Parse an incomplete date character column (d-HH:MM:SS) in R. More elegant approach?


as.POSIXct(paste0("2021-11-", testDate), format = "%Y-%m-%d-%H:%M:%S")

[1] "2021-11-26 16:24:40 CET" "2021-11-26 16:29:40 CET" "2021-11-26 16:34:40 CET" "2021-11-26 16:39:40 CET"
[5] "2021-11-26 16:44:40 CET" "2021-11-26 16:49:40 CET" "2021-11-26 16:54:40 CET" "2021-11-26 16:59:40 CET"
[9] "2021-11-26 17:04:40 CET" "2021-11-26 17:09:40 CET"

Correcting Time Format in R

Here's a solution using lubridate::hms. If you want to use as.POSIXct, substitute your code for that.

It assumes that all values are either MM:SS or HH::MM::SS.

ts1 is the original values, ts2 pre-pended with "00:" where necessary and ts3 the final values.

library(dplyr)
library(stringr)
library(lubridate)

data.frame(ts1 = c("59:34", "32:07", "1:08:06")) %>%
mutate(ts2 = ifelse(str_count(ts1, ":") == 1, paste0("00:", ts1), ts1),
ts3 = hms(ts2))

Result:

      ts1      ts2      ts3
1 59:34 00:59:34 59M 34S
2 32:07 00:32:07 32M 7S
3 1:08:06 1:08:06 1H 8M 6S

How to convert date & time to numeric by using POSIXct and how to calculate the time?

Additionally, you can also use mdy_hm function from lubridate -

library(dplyr)
library(lubridate)

df <- df %>% mutate(across(c(starttime, stoptime), mdy_hm))

In base R, you can use as.POSIXct

df[1:2] <- lapply(df[1:2], as.POSIXct, format = "%m/%d/%Y %H:%M")

How to merge `Date` and `times` columns to obtain a single column in `POSIXct format`


library(data.table)
setDT(df)
df[, datetime := as.POSIXct( as.ITime(Time), as.IDate(Date))]
df[, datetime.1 := as.POSIXct( as.ITime(Time.1), as.IDate(Date.1))]
# Date Time Date.1 Time.1 datetime datetime.1
# 1: 2021-04-07 0.4902778 2021-04-08 0.7548611 2021-04-07 11:46:00 2021-04-08 18:06:59
# 2: 2021-04-07 0.4902778 2021-04-08 0.7548611 2021-04-07 11:46:00 2021-04-08 18:06:59
# 3: 2021-04-08 0.5520833 2021-04-09 0.9375000 2021-04-08 13:14:59 2021-04-09 22:30:00
# 4: 2021-04-08 0.5520833 2021-04-09 0.9375000 2021-04-08 13:14:59 2021-04-09 22:30:00
# 5: 2021-04-09 0.4618056 2021-04-10 0.4722222 2021-04-09 11:05:00 2021-04-10 11:19:59
# 6: 2021-04-09 0.4618056 2021-04-10 0.4722222 2021-04-09 11:05:00 2021-04-10 11:19:59
# 7: 2021-04-09 0.4618056 2021-04-10 0.4722222 2021-04-09 11:05:00 2021-04-10 11:19:59
# 8: 2021-04-14 0.6479167 <NA> NA 2021-04-14 15:33:00 <NA>
# 9: 2021-04-14 0.6479167 <NA> NA 2021-04-14 15:33:00 <NA>
#10: 2021-04-14 0.6479167 <NA> NA 2021-04-14 15:33:00 <NA>


Related Topics



Leave a reply



Submit