Aswathy Rajan
Aswathy Rajan

Reputation: 11

R function to convert chr to time

I have a code below to get time for flight arrivals, i'm trying to plot a graph using time in y axis. How do I convert column value to time?

 library(ggplot2)
 library(hms)

dataset <- tibble::tribble(
  ~flights, ~attribute,                  ~Value,
  "Emirates",      "ETA",       "12:30:00",
  "Emirates", "Arr Time", "14:50:00 PM",
  "Turkish Airlines",      "ETA", "17:30:00 PM",
  "Turkish Airlines", "Arr Time", "18:50:00 PM",
  "Cathay Pacific",      "ETA", "9=19:30:00 PM",
  "Cathay Pacific", "Arr Time", "14:50:00 PM",
  "Qatar Airways",      "ETA", "20:30:00 PM",
  "Qatar Airways", "Arr Time", "20:50:00 PM",
  "Lefthansa",      "ETA",       "12:30:00",
  "Lefthansa", "Arr Time",       "13:50:00"
) 

#dataset$value <- round_hms(as_hms(dataset$Value),digits=5)

str(dataset)

Upvotes: 1

Views: 138

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388817

I would suggest to change the column to POSIXct type. You can do that with parse_date_time function from lubridate. Since there is no date in the data it would use the default date i.e 0000-01-01. While plotting you can ignore the date and plot only time. scale_y_time/scale_y_datetime should help you do that.

dataset$Value <- lubridate::parse_date_time(dataset$Value, 'HMS')

#   flights          attribute Value              
#   <chr>            <chr>     <dttm>             
# 1 Emirates         ETA       0000-01-01 12:30:00
# 2 Emirates         Arr Time  0000-01-01 14:50:00
# 3 Turkish Airlines ETA       0000-01-01 17:30:00
# 4 Turkish Airlines Arr Time  0000-01-01 18:50:00
# 5 Cathay Pacific   ETA       0000-01-01 19:30:00
# 6 Cathay Pacific   Arr Time  0000-01-01 14:50:00
# 7 Qatar Airways    ETA       0000-01-01 20:30:00
# 8 Qatar Airways    Arr Time  0000-01-01 20:50:00
# 9 Lefthansa        ETA       0000-01-01 12:30:00
#10 Lefthansa        Arr Time  0000-01-01 13:50:00

Upvotes: 1

Vin&#237;cius F&#233;lix
Vin&#237;cius F&#233;lix

Reputation: 8811

You can use mutate() from dplyr to create a new variable. A NA will appear, since 9=19:30:00 PM is diffent from others, but you can fix before applying hms

library(dplyr)
library(lubridate)

dataset %>% 
  mutate(time = hms(Value))

# A tibble: 10 x 4
   flights          attribute Value         time      
   <chr>            <chr>     <chr>         <Period>  
 1 Emirates         ETA       12:30:00      12H 30M 0S
 2 Emirates         Arr Time  14:50:00 PM   14H 50M 0S
 3 Turkish Airlines ETA       17:30:00 PM   17H 30M 0S
 4 Turkish Airlines Arr Time  18:50:00 PM   18H 50M 0S
 5 Cathay Pacific   ETA       9=19:30:00 PM NA        
 6 Cathay Pacific   Arr Time  14:50:00 PM   14H 50M 0S
 7 Qatar Airways    ETA       20:30:00 PM   20H 30M 0S
 8 Qatar Airways    Arr Time  20:50:00 PM   20H 50M 0S
 9 Lefthansa        ETA       12:30:00      12H 30M 0S
10 Lefthansa        Arr Time  13:50:00      13H 50M 0S
Warning message:
Problem with `mutate()` column `time`.
i `time = hms(Value)`.
i Some strings failed to parse, or all strings are NAs

Upvotes: 0

Related Questions