converting numbers to time

后端 未结 5 532
小鲜肉
小鲜肉 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 03:59

    I think you don't need the chron package necessarily. When:

    x  <-  c(834, 1534)
    

    Then:

    time <- substr(as.POSIXct(sprintf("%04.0f", x), format='%H%M'), 12, 16)
    time
    
    [1] "08:34" "15:34"
    

    should give you the desired result. When you also want to include a variable which represents the date, you can use the ollowing line of code:

    df$datetime <- as.POSIXct(paste(df$yymmdd, sprintf("%04.0f", df$x)), format='%Y%m%d %H%M%S')
    

提交回复
热议问题