Search code examples
rdatetimeposixctposixlt

How do I extract only the time parameters from Datetime variable in R?


In a R dataframe I have time variable. The data is in the format %a-%b-%d %H:%M:%S. for e.g.,

2015-03-23 20:00:00

I want to get the following data only

  20:00:00

I have created a table basis the above said variable and trying to make a line graph:

                     Var1 Var2  Freq
    1 2015-03-24 00:00:00   RT   612
    2 2015-03-24 01:00:00   RT    65
    3 2015-03-24 06:00:00   RT    58
    4 2015-03-24 07:00:00   RT  5132
    5 2015-03-24 08:00:00   RT  4483
    6 2015-03-24 09:00:00   RT 11112

I have used the following code to make a ggplot line graph:

   library(ggplot2)
   library(stringr)
   ggplot(rtt, aes(x = as.factor(Var1), y = Freq, colour = Var2, group = Var2)) + geom_line(size = 1) +
    xlab("R Vs T") + geom_point() +
    scale_x_discrete(labels = function(x) str_wrap(x, width = 2)) +
    ggtitle("Number of T Vs R - through the day") +
    theme(plot.title=element_text(size=rel(1.2), lineheight = 1 ))

How do I remove the YMD data from this, because I want only the time and not the data in x-axis and the x-axis in the graph looks completely garbled.


Solution

  • There are a number of options to extract the 'time' part. Some are listed below:

     format(as.POSIXct(str1), '%H:%M:%S')
     [1] "20:00:00"
    

    Or

     sub('[^ ]+ ', '', str1)
     #[1] "20:00:00"
    

    Or

     strftime(str1, format='%H:%M:%S')
     #[1] "20:00:00"
    

    Or

     library(lubridate)
     format(ymd_hms(str1), '%H:%M:%S')
     #[1] "20:00:00"
    

    The ggplot code can be changed to

     library(ggplot2)
     ggplot(rtt, aes(x= factor(strftime(Var1, format='%H:%M:%S')),
         y= Freq, colour=Var2, group=Var2)) +
         xlab("R Vs T") +
         geom_point() + 
         scale_x_discrete(labels = function(x) str_wrap(x, width = 2)) +
         ggtitle("Number of T Vs R - through the day") +
         theme(plot.title=element_text(size=rel(1.2), lineheight = 1 ))
    

    Update

    If you need to extract only the 'hour' part

     library(lubridate)
     hour(ymd_hms(str1))
     #[1] 20
    

    data

     str1 <- '2015-03-23 20:00:00'
    
     rtt <- structure(list(Var1 = c("2015-03-24 00:00:00", 
     "2015-03-24 01:00:00", 
     "2015-03-24 06:00:00", "2015-03-24 07:00:00", "2015-03-24 08:00:00", 
     "2015-03-24 09:00:00"), Var2 = c("RT", "RT", "RT", "RT", "RT", 
     "RT"), Freq = c(612L, 65L, 58L, 5132L, 4483L, 11112L)), 
     .Names = c("Var1", "Var2", "Freq"), class = "data.frame",
      row.names = c(NA, -6L))