1

This is what the sample looks like:

# A tibble: 10 x 3
   trip_id start_time end_time
     <dbl> <time>     <time>  
 1       1 20:21:42   21:04:00
 2       2 08:28:42   08:58:42
 3       3 18:41:59   18:50:59
 4       4 10:06:15   11:39:22
 5       5 08:20:27   08:51:33
 6       6 11:11:36   12:12:54
 7       7 19:54:38   20:28:38
 8   25854 11:33:22   12:05:06
 9   25855 07:38:07   07:40:07
10   25856 11:33:03   11:42:02
data <- structure(list(trip_id = c(1, 2, 3, 4, 5, 6, 7, 25854, 25855,25856), start_time = structure(c(73302, 30522, 67319, 36375,30027, 40296, 71678, 41602, 27487, 41583), class = c("hms", "difftime"), units = "secs"), end_time = structure(c(75840, 32322, 67859,41962, 31893, 43974, 73718, 43506, 27607, 42122), class = c("hms","difftime"), units = "secs")), row.names = c(NA, -10L), class = c("tbl_df","tbl", "data.frame")) 

As you can see, my data start_time and end_time columns are chr type. Therefore, I want to convert it into a proper time class and also numerics. My output should be like this;

# A tibble: 10 x 3
   trip_id start_time end_time start_time2 end_time2
     <int> <dttm>      <dttm>  <dbl>       <dbl> 
 1       1 20:21:42   21:04:00 20.21       21.04
 2       2 08:28:42   08:58:42 08.28       08:58
 3       3 18:41:59   18:50:59 18.41       18.50
 4       4 10:06:15   11:39:22 10.06       11.39
 5       5 08:20:27   08:51:33 08.20       08.51 
 6       6 11:11:36   12:12:54 11.11       12.12
 7       7 19:54:38   20:28:38 19.54       20.28 
 8   25854 11:33:22   12:05:06 11.33       12.05
 9   25855 07:38:07   07:40:07 07.38       07.40
10   25856 11:33:03   11:42:02 11.33       11.42

Thank you in advance.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Yasumin
  • 443
  • 2
  • 8

1 Answers1

1

The column type is 'hms' and not character. You can convert it to POSIXct class which will add the 1st date ie. 1970-01-01 to it.

library(dplyr)

data %>%
  mutate(across(ends_with('time'), ~as.POSIXct(., tz = 'UTC')), 
         across(ends_with('time'), 
                ~as.numeric(format(., '%H.%M')), .names = '{col}2'))

#   trip_id start_time          end_time            start_time2 end_time2
#     <dbl> <dttm>              <dttm>                    <dbl>     <dbl>
# 1       1 1970-01-01 20:21:42 1970-01-01 21:04:00       20.2      21.0 
# 2       2 1970-01-01 08:28:42 1970-01-01 08:58:42        8.28      8.58
# 3       3 1970-01-01 18:41:59 1970-01-01 18:50:59       18.4      18.5 
# 4       4 1970-01-01 10:06:15 1970-01-01 11:39:22       10.1      11.4 
# 5       5 1970-01-01 08:20:27 1970-01-01 08:51:33        8.2       8.51
# 6       6 1970-01-01 11:11:36 1970-01-01 12:12:54       11.1      12.1 
# 7       7 1970-01-01 19:54:38 1970-01-01 20:28:38       19.5      20.3 
# 8   25854 1970-01-01 11:33:22 1970-01-01 12:05:06       11.3      12.0 
# 9   25855 1970-01-01 07:38:07 1970-01-01 07:40:07        7.38      7.4 
#10   25856 1970-01-01 11:33:03 1970-01-01 11:42:02       11.3      11.4 
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks, it worked! However, can I change the date from 1970-01-01 to another? – Yasumin May 13 '21 at 12:11
  • 1
    Yes, you can do that. For example to get todays date use `~as.POSIXct(paste(Sys.Date(), .), tz = 'UTC')`. Instead of `Sys.Date()` you can put any date `as.POSIXct(paste('2019-08-08', .), tz = 'UTC')` – Ronak Shah May 13 '21 at 12:13