marcelklib
marcelklib

Reputation: 81

if_else statement returning wrong values

i have a dataframe with this data :

# A tibble: 6 × 3
  rowid Arrival             Depart             
  <int> <dttm>              <dttm>             
1     1 2023-02-11 07:00:00 2023-02-11 17:30:00
2     2 2023-02-13 10:00:00 2023-02-13 18:00:00
3     3 2023-02-14 08:00:00 2023-02-14 17:00:00
4     4 2023-02-15 08:00:00 2023-02-15 17:00:00
5     5 2023-02-16 08:00:00 2023-02-16 18:00:00
6     6 2023-02-18 07:00:00 2023-02-18 17:30:00


structure(list(rowid = 1:6, Arrival = structure(c(1676098800, 
1676282400, 1676361600, 1676448000, 1676534400, 1676703600), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), Depart = structure(c(1676136600, 1676311200, 1676394000, 
1676480400, 1676570400, 1676741400), tzone = "UTC", class = c("POSIXct", 
"POSIXt"))), row.names = c(NA, -6L), class = c("tbl_df", "tbl", 
"data.frame"))

i set the following objects :

ri <- 2
int <- test_int[2]

int (an interval) becomes :

> int
[1] 2023-02-13 06:00:00 EST--2023-02-13 13:00:00 EST

and then run this code :

test <- test %>% mutate(
  interval_start = if_else(rowid == ri, int_start(int), Arrival),
  interval_end = if_else(rowid == ri, int_end(int), Depart)
) %>% select(Arrival, interval_start, Depart, interval_end)  

the result is this :

# A tibble: 6 × 4
  Arrival             interval_start      Depart              interval_end       
  <dttm>              <dttm>              <dttm>              <dttm>             
1 2023-02-11 07:00:00 2023-02-11 02:00:00 2023-02-11 17:30:00 2023-02-11 12:30:00
2 2023-02-13 10:00:00 2023-02-13 06:00:00 2023-02-13 18:00:00 2023-02-13 13:00:00
3 2023-02-14 08:00:00 2023-02-14 03:00:00 2023-02-14 17:00:00 2023-02-14 12:00:00
4 2023-02-15 08:00:00 2023-02-15 03:00:00 2023-02-15 17:00:00 2023-02-15 12:00:00
5 2023-02-16 08:00:00 2023-02-16 03:00:00 2023-02-16 18:00:00 2023-02-16 13:00:00
6 2023-02-18 07:00:00 2023-02-18 02:00:00 2023-02-18 17:30:00 2023-02-18 12:30:00


structure(list(Arrival = structure(c(1676098800, 1676282400, 
1676361600, 1676448000, 1676534400, 1676703600), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), interval_start = structure(c(1676098800, 1676286000, 
1676361600, 1676448000, 1676534400, 1676703600), class = c("POSIXct", 
"POSIXt")), Depart = structure(c(1676136600, 1676311200, 1676394000, 
1676480400, 1676570400, 1676741400), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), interval_end = structure(c(1676136600, 1676311200, 
1676394000, 1676480400, 1676570400, 1676741400), class = c("POSIXct", 
"POSIXt"))), row.names = c(NA, -6L), class = c("tbl_df", "tbl", 
"data.frame"))

for some reason the if_else statement is returning some weird times instead of the values for arrival/depart, but it is correctly mutating rowid 2 to the correct time.

anyone knows why this could be and how i can fix it?

Upvotes: 0

Views: 30

Answers (1)

marcelklib
marcelklib

Reputation: 81

thanks to @George Savva, i was able to see that the erroneous values i was getting was because my int variable was in the EST timezone instead of UTC which is why there was a 5 hour difference

Upvotes: 1

Related Questions