3

I have this dataframe:

df <- data.frame(datetime = c("2018-08-23 11:03:25 0300", "2018-08-17 12:54:09 0300", "2018-08-07 17:15:29 0400", "2018-08-07 10:41:30 0400", "2018-08-07 10:37:37 0400", "2018-08-03 10:36:49 0400", "2018-07-26 12:52:28 0400", "2018-07-10 18:06:02 0400", "2018-07-04 17:52:24 0400", "2018-06-20 15:28:08 0400"),
           stringsAsFactors = FALSE)
> df
                       datetime
1  2018-08-23 11:03:25 0300
2  2018-08-17 12:54:09 0300
3  2018-08-07 17:15:29 0400
4  2018-08-07 10:41:30 0400
5  2018-08-07 10:37:37 0400
6  2018-08-03 10:36:49 0400
7  2018-07-26 12:52:28 0400
8  2018-07-10 18:06:02 0400
9  2018-07-04 17:52:24 0400
10 2018-06-20 15:28:08 0400

I need to transform it in the same timezone and delete its timezone (why? because It will make easier to make some computations of hours/days/weeks between vectors of the same type)

Expected Output:

              datetime
1  2018-08-23 12:03:25    # this was UTC +3, now +1 hour is UTC +4
2  2018-08-17 13:54:09    # this was UTC +3, now +1 hour is UTC +4
3  2018-08-07 17:15:29    # this is UTC +4
4  2018-08-07 10:41:30    # this is UTC +4
5  2018-08-07 10:37:37    # this is UTC +4
6  2018-08-03 10:36:49    # this is UTC +4
7  2018-07-26 12:52:28    # this is UTC +4
8  2018-07-10 18:06:02    # this is UTC +4
9  2018-07-04 17:52:24    # this is UTC +4
10 2018-06-20 15:28:08    # this is UTC +4

Any ideas?

EDIT:

This question is by place, and its solved by the argument tz as you can insert the place there. However, in this question I don't have an argument to recognize the timezone inside the string.

Chris
  • 2,019
  • 5
  • 22
  • 67
  • `lubridate::ydm_hms(date, tz = "your_time_zone")` Where `your_time_zone` is the result of `Sys.timezone()` – Matias Andina Apr 01 '19 at 21:08
  • 1
    Possible duplicate of [How do you convert dates/times from one time zone to another in R?](https://stackoverflow.com/questions/1395117/how-do-you-convert-dates-times-from-one-time-zone-to-another-in-r) – divibisan Apr 02 '19 at 17:44

2 Answers2

2

This works,

Using library(tidyverse) and library(stringr),

df <- data.frame(datetime = c("2018-08-23 11:03:25 0300", "2018-08-17 12:54:09 0300", "2018-08-07 17:15:29 0400", "2018-08-07 10:41:30 0400", "2018-08-07 10:37:37 0400", "2018-08-03 10:36:49 0400", "2018-07-26 12:52:28 0400", "2018-07-10 18:06:02 0400", "2018-07-04 17:52:24 0400", "2018-06-20 15:28:08 0400"),
                     stringsAsFactors = FALSE)

df2 <-  as.tibble(stringr::str_split_fixed(df$datetime, " ", 3)) %>% 
  mutate(V3 = -((as.numeric(V3)/100)-4)) %>% 
  mutate(V4 = as.numeric(stringr::str_split_fixed(V2, ":", 3)[,1]) + V3) %>% 
  mutate(V5 = stringr::str_split_fixed(.$V2, ":", 3)[,2]) %>% 
  mutate(V6 = stringr::str_split_fixed(.$V2, ":", 3)[,3]) %>% 
  mutate(V2 = paste(V4, V5, V6, sep = ":")) %>% 
  mutate(datetime = paste(V1, V2, sep = " ")) %>% 
  dplyr::select(datetime)

Some explanation, first we split the datetime column into 3 parts, then we change the timezone parameter, into something we can just add. Then split the time and the date strings, add the timezone parameter, then paste it all back together. This can be done in less lines, but you can follow the steps well by running them one at a time.

Hector Haffenden
  • 1,360
  • 10
  • 25
2

A solution using the tidyverse and lubridate package.

library(tidyverse)
library(lubridate)

df <- data.frame(datetime = c("2018-08-23 11:03:25 0300", "2018-08-17 12:54:09 0300", "2018-08-07 17:15:29 0400", "2018-08-07 10:41:30 0400", "2018-08-07 10:37:37 0400", "2018-08-03 10:36:49 0400", "2018-07-26 12:52:28 0400", "2018-07-10 18:06:02 0400", "2018-07-04 17:52:24 0400", "2018-06-20 15:28:08 0400"),
                 stringsAsFactors = FALSE)

df2 <- df %>%
  separate(datetime, into = c("date", "time", "timezone"), sep = " ") %>%
  unite(col = datetime, c("date", "time"), sep = " ") %>%
  mutate(datetime = ymd_hms(datetime)) %>%
  mutate(datetime = if_else(timezone %in% "0300", datetime + hours(1), datetime)) %>%
  select(-timezone)
df2
#               datetime
# 1  2018-08-23 12:03:25
# 2  2018-08-17 13:54:09
# 3  2018-08-07 17:15:29
# 4  2018-08-07 10:41:30
# 5  2018-08-07 10:37:37
# 6  2018-08-03 10:36:49
# 7  2018-07-26 12:52:28
# 8  2018-07-10 18:06:02
# 9  2018-07-04 17:52:24
# 10 2018-06-20 15:28:08
www
  • 38,575
  • 12
  • 48
  • 84