converting numbers to time

后端 未结 5 521
小鲜肉
小鲜肉 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:15

    Say

    x  <-  c('834', '1534')
    

    The last two characters represent minutes, so you can extract them using

    mins  <-  substr(x, nchar(x)-1, nchar(x))
    

    Similarly, extract hours with

    hour  <-  substr(x, 0, nchar(x)-2)
    

    Then create a fixed vector of time values with

    time  <-  paste0(hour, ':', mins)
    

    I think you are forced to specify dates in the chron package, so assuming a date value, you can converto chron with this:

    chron(dates.=rep('02/02/02', 2), 
          times.=paste0(hour, ':', mins, ':00'), 
          format=c(dates='m/d/y',times='h:m:s'))
    

提交回复
热议问题