time categorisation

appointments$AppointmentRegistration=as.Date(appointments$AppointmentRegistration,format="%y-%m-%dT%h:%m:%sZ")

#2015-08-07T08:19:19Zformat="%y-%m-%d","T0","%h:%m:%s"

appointments$Houroftheday=hour(appointments$AppointmentRegistration)

above are the codes i runned

can someone help me categorizing the time formate according to hour of the day according to formate 2015-08-07T08:19:19Z

Hi @Astik_Chawda,
Welcome to the RStudio Community Forum.

With a few tweaks your code gives this:

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

appointments <- data.frame(AppointmentRegistration = c("2015-08-07T08:19:19Z",
                                                         "2015-08-15T10:19:19Z",
                                                         "2015-08-31T14:19:19Z"))
appointments
#>   AppointmentRegistration
#> 1    2015-08-07T08:19:19Z
#> 2    2015-08-15T10:19:19Z
#> 3    2015-08-31T14:19:19Z

# Had to change format to include %Y (not %y) and %M (not %m) and %H (not %h)
# as.Date() is not sufficient here as it doesn't keep the "time" part.
appointments$DateTimeOfAR <- as.POSIXct(appointments$AppointmentRegistration,
                                                         format="%Y-%m-%dT%H:%M:%SZ")

# Can now extract the hour alone
appointments$Houroftheday <- hour(appointments$DateTimeOfAR)

appointments
#>   AppointmentRegistration        DateTimeOfAR Houroftheday
#> 1    2015-08-07T08:19:19Z 2015-08-07 08:19:19            8
#> 2    2015-08-15T10:19:19Z 2015-08-15 10:19:19           10
#> 3    2015-08-31T14:19:19Z 2015-08-31 14:19:19           14

Created on 2022-06-09 by the reprex package (v2.0.1)
Hope this helps.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.