Home > OS >  Use of as.POSIXct on missing seconds
Use of as.POSIXct on missing seconds

Time:10-07

Sample dataset

df <- data.frame (dt1 = c("31.07.2022 23:57:06", "01.08.2022 00:02", "01.08.2022 00:07"), 
                  factor = c(21.59,21.75,21.375))

Tworzę kolumnę z datą i czasem

df <- df %>% mutate (DT = as.POSIXct(dt1, format="%d.%m.%Y %H:%M:%S"))

I get the following result:

                  dt1 factor                  DT
1 31.07.2022 23:57:06 21.590 2022-07-31 23:57:06
2    01.08.2022 00:02 21.750                <NA>
3    01.08.2022 00:07 21.375                <NA>

I would like the end result to be as follows

                  dt1 factor                  DT
1 31.07.2022 23:57:06 21.590 2022-07-31 23:57:06
2    01.08.2022 00:02 21.750 01.08.2022 00:02:00
3    01.08.2022 00:07 21.375 01.08.2022 00:07:00

I don't want to waste the information I have, so the deletion of seconds solution is not good

df <- df %>% mutate (DT = as.POSIXct(dt1, format="%d.%m.%Y %H:%M"))
                  dt1 factor                  DT
1 31.07.2022 23:57:06 21.590 2022-07-31 23:57:00
2    01.08.2022 00:02 21.750 2022-08-01 00:02:00
3    01.08.2022 00:07 21.375 2022-08-01 00:07:00

CodePudding user response:

You can use lubridate::dmy_hms with truncated = 1 to deal with incomplete dates:

library(lubridate)
df %>% 
  mutate(DT = dmy_hms(dt1, truncated = 1))
#                   dt1 factor                  DT
# 1 31.07.2022 23:57:06 21.590 2022-07-31 23:57:06
# 2    01.08.2022 00:02 21.750 2022-08-01 00:02:00
# 3    01.08.2022 00:07 21.375 2022-08-01 00:07:00

See the details on the truncated parameter:

The most common type of irregularity in date-time data is the truncation due to rounding or unavailability of the time stamp. If the truncated parameter is non-zero, the ymd_hms() functions also check for truncated formats. For example, ymd_hms() with truncated = 3 will also parse incomplete dates like ⁠2012-06-01 12:23⁠, ⁠2012-06-01 12⁠ and 2012-06-01.

  • Related