I am able to change the time zone of a datetime column using lubridate syntax very easily, see df below.
But when I try to use a base R approach (df2 in reprex) using lapply
and attr(x, "tzone) <- ""
inside an anonymous function it just assigns an emtpy string to each variable.
I would normally use lubridate as it is much easier but trying to develop a function for a package and reduce reliance on dependancies and cannot find anywhere dealing with this specific issue. I am sure I am missing something simple having looked at it so long.
Thanks
library(tidyverse)
df <- tibble::tribble(
~DTSTART.TZID.Europe.London, ~DTEND.TZID.Pacific.Auckland,
"2023-04-06 12:00:00", "2023-04-06 20:00:00",
"2023-04-06 14:00:00", "2023-04-06 22:00:00"
)
df %>%
mutate(
DTSTART.TZID.Europe.London = as.POSIXct(DTSTART.TZID.Europe.London, tz = "Europe/London"),
DTEND.TZID.Pacific.Auckland = as.POSIXct(DTEND.TZID.Pacific.Auckland, tz = "Pacific/Auckland"),
DTEND.TZID.Pacific.Auckland = lubridate::with_tz(DTEND.TZID.Pacific.Auckland, tzone = "")
)
#> # A tibble: 2 × 2
#> DTSTART.TZID.Europe.London DTEND.TZID.Pacific.Auckland
#> <dttm> <dttm>
#> 1 2023-04-06 12:00:00 2023-04-06 09:00:00
#> 2 2023-04-06 14:00:00 2023-04-06 11:00:00
df2 <- df %>%
mutate(
DTSTART.TZID.Europe.London = as.POSIXct(DTSTART.TZID.Europe.London, tz = "Europe/London"),
DTEND.TZID.Pacific.Auckland = as.POSIXct(DTEND.TZID.Pacific.Auckland, tz = "Pacific/Auckland")
)
lapply(df2["DTEND.TZID.Pacific.Auckland"], function(x) {attr(x, "tzone") <- ""})
#> $DTEND.TZID.Pacific.Auckland
#> [1] ""
Created on 2024-04-30 with reprex v2.1.0