How can I convert the schedtime and deptime columns which are in numeric class to time class?

str(flightdelays)
tibble [2,201 × 13] (S3: tbl_df/tbl/data.frame)
 $ schedtime   : num [1:2201] 1455 1640 1245 1715 1039 ...
 $ carrier     : chr [1:2201] "OH" "DH" "DH" "DH" ...
 $ deptime     : num [1:2201] 1455 1640 1245 1709 1035 ...
 $ dest        : chr [1:2201] "JFK" "JFK" "LGA" "LGA" ...
 $ distance    : num [1:2201] 184 213 229 229 229 228 228 228 228 228 ...
 $ date        : chr [1:2201] "37987" "37987" "37987" "37987" ...
 $ flightnumber: num [1:2201] 5935 6155 7208 7215 7792 ...
 $ origin      : chr [1:2201] "BWI" "DCA" "IAD" "IAD" ...
 $ weather     : num [1:2201] 0 0 0 0 0 0 0 0 0 0 ...
 $ dayweek     : num [1:2201] 4 4 4 4 4 4 4 4 4 4 ...
 $ daymonth    : num [1:2201] 1 1 1 1 1 1 1 1 1 1 ...
 $ tailnu      : chr [1:2201] "N940CA" "N405FJ" "N695BR" "N662BR" ...
 $ delay       : chr [1:2201] "ontime" "ontime" "ontime" "ontime" ...
> 

sum((is.na(flightdelays)))
[1] 0

How can I convert the schedtime and deptime columns which are in numeric class to time class?

I have tried sprintf() and it didn't work. Please let me know. Thank you.

Base R doesnt have a pure time class (i..e one that doesnt involve dates also).
but you can use a package to extend the functionality.
for example for time we can use hms package.

library(tidyverse)
library(hms)

(flightdf<- tibble(
  dig4times = c(1455 ,1640)
))

(fin <- mutate(flightdf,
       nice_times = hms(hours=dig4times %/% 100,
                        minutes= dig4times %% 100)))

This topic was automatically closed 42 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.