how do you pick the hour, minute and second from the posixct formated datetime in R

前端 未结 2 1170
清歌不尽
清歌不尽 2021-01-29 09:09

I have a data frame like this:

dput(tail(x,10))
structure(list(DATE = structure(c(1375725600, 1375729200, 1375732800, 
1375736400, 1375740000, 1375743600, 137574         


        
相关标签:
2条回答
  • 2021-01-29 09:11

    this worked:

    x$t<-strftime(x$DATE, format="%H:%M:%S")
    
    0 讨论(0)
  • 2021-01-29 09:19

    You can use lubridate to get the components in hours, minutes and seconds of your dates:

    library(lubridate)
    transform(x,time.str = format(DATE,'%H:%M:%S'),
                  hour = hour(DATE),
                  minute = minute(DATE),
                  second= second(DATE))
    
                        DATE  VOLUME    PC time.str hour minute second
    3399 2013-08-05 20:00:00 2790797 10.12 20:00:00   20      0      0
    3400 2013-08-05 21:00:00 2670868  9.95 21:00:00   21      0      0
    3401 2013-08-05 22:00:00 2586397  9.90 22:00:00   22      0      0
    3402 2013-08-05 23:00:00 2470043  9.46 23:00:00   23      0      0
    3403 2013-08-06 00:00:00 2336334  8.99 00:00:00    0      0      0
    3404 2013-08-06 01:00:00 2228206  8.94 01:00:00    1      0      0
    3405 2013-08-06 02:00:00 2129270  9.79 02:00:00    2      0      0
    3406 2013-08-06 03:00:00 2032518  8.45 03:00:00    3      0      0
    3407 2013-08-06 04:00:00  872133  8.85 04:00:00    4      0      0
    3408 2013-08-06 05:00:00  725377  8.50 05:00:00    5      0      0
    
    0 讨论(0)
提交回复
热议问题