how to convert the time column which is in numeric class to a time class in R studio

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" ...

Problem :- Code and Errors

light_dataset$deptime <- hm(sprintf("%04d",flight_dataset$deptime))
Error in sprintf("%04d", flight_dataset$deptime) :
invalid format '%04d'; use format %s for character objects

View(flight_dataset)

?sprintf()
flight_dataset$deptime <- sprintf("%02d",flight_dataset$deptime)
Error in sprintf("%02d", flight_dataset$deptime) :
invalid format '%02d'; use format %s for character objects
View(flight_dataset)

flight_dataset$date <- as.Date(flight_dataset$date, format = "%y%m%d")
Error in if (nchar(current) > nchars) { :
missing value where TRUE/FALSE needed
print(flight_dataset$date)

class(flight_dataset$schedtime)
[1] "numeric"

library(lubridate)
flight_dataset$schedtime <- hm(sprintf(""%04d",flight_dataset$numeric_schedtime))
Error: unexpected input in "flight_dataset$schedtime <- hm(sprintf(""%04d",flight_dataset$numeric_schedtime))"
View(flight_dataset)

flight_dataset$schedtime <- hm(sprintf("%O4d",flight_dataset$schedtime))
Error in sprintf("%O4d", flight_dataset$schedtime) :
invalid format '%O4d'; use format %f, %e, %g or %a for numeric objects
View(flight_dataset)

Please help me ...I am a beginner and learning R

Please post the output of

dput(head(flight_dataset))

Put three back ticks just before and after the pasted data, like this:
```
output of dput() goes here
```

dput(head(flight_dataset))
structure(list(schedtime = new("Period", .Data = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), year = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), month = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), day = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), hour = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), minute = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), carrier = c("OH",
"DH", "DH", "DH", "DH", "DH"), deptime = c("1455", "1640", "1245",
"1709", "1035", "0839"), dest = c("JFK", "JFK", "LGA", "LGA",
"LGA", "JFK"), distance = c(184, 213, 229, 229, 229, 228), date = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), class = "Date"),
flightnumber = c(5935, 6155, 7208, 7215, 7792, 7800), origin = c("BWI",
"DCA", "IAD", "IAD", "IAD", "IAD"), weather = c(0, 0, 0,
0, 0, 0), dayweek = c(4, 4, 4, 4, 4, 4), daymonth = c(1,
1, 1, 1, 1, 1), tailnu = c("N940CA", "N405FJ", "N695BR",
"N662BR", "N698BR", "N687BR"), delay = c("ontime", "ontime",
"ontime", "ontime", "ontime", "ontime")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))

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" ...

I want to change columns like schedtime, and deptime, from numeric class to time and date column to yyyy/mm/dd I am a beginner trying to learn R. Please let me know.

It seems you have started a new thread: How can I convert the schedtime and deptime columns which are in numeric class to time class?

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.