Date time conversion and extract only time

前端 未结 5 923
暖寄归人
暖寄归人 2020-12-09 08:36

Want to change the class for Time to POSIXlt and extract only the hours minutes and seconds

str(df3$Time)
chr [1:2075259] \"17:24:00\" \"17:25:00\" \"17:26:0         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-09 09:19

    If your data is

    a <- "17:24:00"
    
    b <- strptime(a, format = "%H:%M:%S")
    

    you can use lubridate in order to have a result of class integer

    library(lubridate)
    hour(b)
    minute(b)
    
    # > hour(b)
    # [1] 17
    # > minute(b)
    # [1] 24
    
    
    # > class(minute(b))
    # [1] "integer"
    

    and you can combine them using

    # character
    paste(hour(b),minute(b), sep=":")
    
    # numeric
    hour(b) + minute(b)/60
    

    for instance.

    I would not advise to do that if you want to do any further operations on your data. However, it might be convenient to do that if you want to plot the results.

提交回复
热议问题