converting numbers to time

后端 未结 5 523
小鲜肉
小鲜肉 2020-12-18 03:49

I entered my data by hand, and to save time I didn\'t include any punctuation in my times. So, for example, 8:32am I entered as 832. 3:34pm I entered as 1534. I\'m trying to

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-18 04:02

    I thought I'd throw out a non-regex solution that uses lubridate. This is probably overkill.

    library(lubridate)
    library(stringr)
    
    time.orig <- c('834', '1534')
    
    # zero pad times before noon
    time.padded <- str_pad(time.orig, 4, pad="0")
    
    # parse using lubridate
    time.period <- hm(time.padded)
    
    # make it look like time
    time.pretty <- paste(hour(time.period), minute(time.period), sep=":")
    

    And you end up with

    > time.pretty
    [1] "8:34"  "15:34"
    

提交回复
热议问题