Yasumin
Yasumin

Reputation: 453

How to convert time format in numeric in R

This is what the sample looks like:

# A tibble: 10 x 3
   trip_id start_time          end_time           
     <int> <dttm>              <dttm>             
 1       1 2020-10-05 11:11:36 2020-10-05 12:12:54
 2       2 2020-10-05 16:09:16 2020-10-05 20:00:42
 3       3 2020-10-05 09:16:33 2020-10-05 11:16:27
 4       4 2020-10-05 14:16:38 2020-10-05 14:37:38
 5       5 2020-10-05 13:08:16 2020-10-05 13:13:16
 6       6 2020-10-05 11:02:23 2020-10-05 13:04:16
 7       7 2020-10-05 13:15:19 2020-10-05 15:54:19
 8   56562 2020-10-09 11:05:25 2020-10-09 13:37:44
 9   56563 2020-10-09 14:11:30 2020-10-09 14:12:30
10   56564 2020-10-09 16:00:40 2020-10-09 16:46:58
data <- structure(list(trip_id = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 56562L,56563L, 56564L), start_time = structure(c(1601896296, 1601914156,1601889393, 1601907398, 1601903296, 1601895743, 1601903719, 1602241525,1602252690, 1602259240), tzone = "UTC", class = c("POSIXct","POSIXt")), end_time = structure(c(1601899974, 1601928042, 1601896587,1601908658, 1601903596, 1601903056, 1601913259, 1602250664, 1602252750,1602262018), tzone = "UTC", class = c("POSIXct", "POSIXt"))), row.names = c(NA,-10L), class = c("tbl_df", "tbl", "data.frame")) 

Based on my question above, I want to create other columns which convert my time columns into a numeric value. I think there are many ways to convert it. For example, it may take the second from 0 o'clock or an hour from 0 o'clock or a minute from 0 o'clock. 6 o'clock is represented as 3600 seconds from 0 o'clock.

Therefore, any package and function can do this? Is there another appropriate way to convert my time columns data to numeric values? I want to focus only on my time (dates are no count). Thank you in advance for any help.

Upvotes: 2

Views: 1188

Answers (3)

TarJae
TarJae

Reputation: 79204

We could use lubridate.

library(dplyr)
library(lubridate)
df %>% 
  mutate(across(ends_with("time"),  ymd_hms)) %>% 
  mutate(across(ends_with("time"), ~ as.numeric(.), .names = "{.col}.{.fn}"))

Output:

   trip_id start_time          end_time            start_time.1 end_time.1
     <dbl> <dttm>              <dttm>                     <dbl>      <dbl>
 1       1 2020-10-05 11:11:36 2020-10-05 12:12:54   1601896296 1601899974
 2       2 2020-10-05 16:09:16 2020-10-05 20:00:42   1601914156 1601928042
 3       3 2020-10-05 09:16:33 2020-10-05 11:16:27   1601889393 1601896587
 4       4 2020-10-05 14:16:38 2020-10-05 14:37:38   1601907398 1601908658
 5       5 2020-10-05 13:08:16 2020-10-05 13:13:16   1601903296 1601903596
 6       6 2020-10-05 11:02:23 2020-10-05 13:04:16   1601895743 1601903056
 7       7 2020-10-05 13:15:19 2020-10-05 15:54:19   1601903719 1601913259
 8   56562 2020-10-09 11:05:25 2020-10-09 13:37:44   1602241525 1602250664
 9   56563 2020-10-09 14:11:30 2020-10-09 14:12:30   1602252690 1602252750
10   56564 2020-10-09 16:00:40 2020-10-09 16:46:58   1602259240 1602262018

Upvotes: 4

Claudio
Claudio

Reputation: 1538

They are numbers, just convert them using as.number: the result is the number of seconds since 1970-01-01 00:00. If you want to check:

data$start_time[1]
[1] "2020-10-05 11:11:36 UTC"

as.numeric(data$start_time)[1]
[1] 1601896296

as.POSIXct(1601896296, tz="UTC", origin="1970-01-01")
[1] "2020-10-05 11:11:36 UTC"

Upvotes: 3

aryashah2k
aryashah2k

Reputation: 538

The below line of code will help convert the time difference between a sample start time and end time into seconds.

You can make use of this and modify according to your needs. the gist being you will be using as.numeric method and as.POSIXct

as.numeric(as.POSIXct("2007-05-17 09:00:00")-as.POSIXct("2007-05-07 09:00:00"), units="secs")

Upvotes: 2

Related Questions