How can I keep midnight (00:00h) using strptime() in R?

后端 未结 2 1476
庸人自扰
庸人自扰 2020-12-03 20:04

I have a dataframe, df, which has factor variable for date in the following format:

2015-12-15 10:00:00
2015-12-19 12:00:00
2015-12-20 20:00:00
相关标签:
2条回答
  • 2020-12-03 20:52

    If we have a vector like "v1", by using strptime we get NA for those elements that don't have the correct format

    strptime(v1,  "%d/%m/%Y %H:%M:%S", tz = "UTC")
    #[1] "2015-12-19 12:00:00 UTC" NA  
    

    One way to correct this will be to paste the "00:00:00" string for those that doesn't have that

    v1[!grepl(":", v1)] <- paste(v1[!grepl(":", v1)], "00:00:00") 
    strptime(v1,  "%d/%m/%Y %H:%M:%S", tz = "UTC")
    #[1] "2015-12-19 12:00:00 UTC" "2015-12-19 00:00:00 UTC"
    

    Or if we use lubridate, the parse_date_time can take multiple formats

    library(lubridate)
    parse_date_time(v1, guess_formats(v1, c("%d/%m/%Y %H:%M:%S", "%d/%m/%Y")))
    #[1] "2015-12-19 12:00:00 UTC" "2015-12-19 00:00:00 UTC"
    

    data

    v1 <- c("19/12/2015 12:00:00", "19/12/2015") 
    
    0 讨论(0)
  • 2020-12-03 21:00

    From R's strptime documentation (emphasis added):

    format

    A character string. The default for the format methods is "%Y-%m-%d %H:%M:%S" if any element has a time component which is not midnight, and "%Y-%m-%d" otherwise. If options("digits.secs") is set, up to the specified number of digits will be printed for seconds.

    So the information is still there, you just need to format it to print it out with the time components.

    > midnight <- strptime("2015-12-19 00:00:00","%Y-%m-%d %H:%M")
    > midnight
    [1] "2015-12-19 EST"
    > format(midnight,"%Y/%m/%d %H:%M")
    [1] "2015/12/19 00:00"
    
    0 讨论(0)
提交回复
热议问题