Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

datetime format - R mdy_hms unpredictable results?

Working with the mdy_hms function on some data I have and am running into an interesting problem. I'm getting data uploaded from many sources, but they all should be in csv format and conform to the same guidelines, so they SHOULD all be identical formats.

I have 2 variables.

> good_time
[1] "12/28/2019 16:22"
> test_time
[1] "3/4/2020 16:46"
> str(good_time)
chr "12/28/2019 16:22"
> str(test_time)
chr "3/4/2020 16:46"

So they appear to be the same to me in terms of formatting, but good_time can parse just fine via mdy_hms whereas test_time cannot. Can anyone explain to me why?

> mdy_hms(good_time)
[1] "2020-12-28 19:16:22 UTC"
> mdy_hms(test_time)
[1] NA
Warning message:
All formats failed to parse. No formats found.

Oddly enough if I use mdy_hm(test_time) it works fine.

> mdy_hm(test_time)
[1] "2020-03-04 16:46:00 UTC"
question from:https://stackoverflow.com/questions/66065885/r-mdy-hms-unpredictable-results

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

lubridate is expecting leading zeroes in single-digit months (and days).

From ?lubridate::mdy_hms:

truncated: integer, indicating how many formats can be missing. See
          details.

...

     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'. NOTE: The 'ymd()' family of
     functions is based on 'base::strptime()' which currently fails to
     parse %y-%m formats.

Just add truncated=1:

lubridate::mdy_hms("3/4/2020 16:46", truncated=1)
# [1] "2020-03-04 16:46:00 UTC"

(This was also discussed in tidyverse/lubridate#669.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...