R语言 if_else语句返回错误值

siv3szwd  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(103)

我有一个包含以下数据的 Dataframe :

# 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"))

我设置了以下对象:

ri <- 2
int <- test_int[2]

int(间隔)变为:

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

然后运行以下代码:

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)

结果是这样的:

# 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"))

由于某种原因,if_else语句返回了一些奇怪的时间,而不是arrival/devit的值,但是它正确地将rowid2修改为正确的时间。
有人知道为什么会这样吗?我该怎么解决?

camsedfj

camsedfj1#

多亏了@乔治Savva,我才能看到我得到的错误值是因为我的int变量在EST时区而不是UTC时区,这就是为什么有5小时的差异

相关问题