'Change the time zone only for certain rows in a column?

I am working with data in R and would like to change the time zone of some POSIXct data, but only for certain rows within the columns (Survey_Start and Survey_End). Some of the data is already in the proper time zone, so converting the entire column is a problem. My code to change the time zone is:

    herps2021 <- herps2021 %>%
    mutate(Survey_Start = as.POSIXct(Survey_Start, format = "%H:%M:%S", 
    tz = "UTC"),
     Survey_End = as.POSIXct(Survey_End, format = "%H:%M:%S", tz = 
    "UTC"),
         
          #Change to proper time zone
         Survey_Start = with_tz(Survey_Start, tzone = "America/Los_Angeles"),
         Survey_End  = with_tz(Survey_End , tzone = "America/Los_Angeles")
          )

Is there a way to specify which rows for the columns Survey_Start and Survey_End I want to convert, so that the data already in the correct time zone is unaffected?

Thanks!

r


Solution 1:[1]

you could try using parse_date_time that allows you to parse multiple dates and times in a column.

Looks something like this:

library(lubridate)
parse_date_time(c("2016", "2016-04"), orders = c("Y", "Ym"))
#> [1] "2016-01-01 UTC" "2016-04-01 UTC"

here is the link to the documentation: https://lubridate.tidyverse.org/reference/parse_date_time.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alexb523