尝试结合日期和时间

Trying to combine dates and times

我正在尝试合并日期和时间。这些来自导入时的文件,如下所示:

library(tidyverse)
library(lubridate)

bookings <- structure(list(booking_date = structure(c(1549670400, 1550275200, 
    1550880000, 1551484800, 1552089600, 1552694400), class = c("POSIXct", 
    "POSIXt"), tzone = "UTC"), start_time = structure(c(-2209043700, 
    -2209043700, -2209043700, -2209043700, -2209043700, -2209043700
    ), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA, 
    -6L), class = c("tbl_df", "tbl", "data.frame"))

看起来像这样:

# A tibble: 6 x 2
  booking_date        start_time         
  <dttm>              <dttm>             
1 2019-02-09 00:00:00 1899-12-31 08:45:00
2 2019-02-16 00:00:00 1899-12-31 08:45:00
3 2019-02-23 00:00:00 1899-12-31 08:45:00
4 2019-03-02 00:00:00 1899-12-31 08:45:00
5 2019-03-09 00:00:00 1899-12-31 08:45:00
6 2019-03-16 00:00:00 1899-12-31 08:45:00

显然 start_time 栏中的日期是错误的。它应该与预订日期结合在一起,因此第一行应该显示为 2019-02-09 08:45:00.

最好的方法是什么?我试过这个 (based on this other answer),这在我的情况下并不奏效。

bookings %>% 
  select(booking_date, start_time) %>% 
  mutate(time_2 = as.character(start_time)) %>% 
  mutate(time_3 = str_sub(time_2, -8, -1)) %>% 
  mutate(booking_start = dmy(paste(booking_date, time_3)))

谢谢。

如果您想从 booking_date 获取 start_time 的日期,基本的 R 方法是 paste "Date" 从 booking_date 和 "time" 来自 start_time 的部分并将它们转换为 POSIXct.

bookings$start_time <- as.POSIXct(paste(as.Date(bookings$booking_date), 
                                  format(bookings$start_time, "%T")))

bookings
# A tibble: 6 x 2
#  booking_date        start_time         
#  <dttm>              <dttm>             
#1 2019-02-09 00:00:00 2019-02-09 08:45:00
#2 2019-02-16 00:00:00 2019-02-16 08:45:00
#3 2019-02-23 00:00:00 2019-02-23 08:45:00
#4 2019-03-02 00:00:00 2019-03-02 08:45:00
#5 2019-03-09 00:00:00 2019-03-09 08:45:00
#6 2019-03-16 00:00:00 2019-03-16 08:45:00

如果你想在管道中使用它,你可以这样做

library(dplyr)
bookings %>%
  mutate(start_time = as.POSIXct(paste(as.Date(booking_date), 
                      format(start_time, "%T"))))

我们也可以用 lubridate::date 来做到这一点。

date() <- 允许您设置 date/time 对象的日期组件:

# Set the date component of start_time to be the date component of booking_date
date(bookings$start_time) <- bookings$booking_date

bookings

# A tibble: 6 x 2
  booking_date        start_time         
  <dttm>              <dttm>             
1 2019-02-09 00:00:00 2019-02-09 08:45:00
2 2019-02-16 00:00:00 2019-02-16 08:45:00
3 2019-02-23 00:00:00 2019-02-23 08:45:00
4 2019-03-02 00:00:00 2019-03-02 08:45:00
5 2019-03-09 00:00:00 2019-03-09 08:45:00
6 2019-03-16 00:00:00 2019-03-16 08:45:00

由于它使用赋值 (<-),因此您不能将第一种方法用作管道的一部分。在管道中起作用的是 update.POSIXt 方法(参见 ?DateTimeUpdate),它允许您更新日期的日期组件,但您必须具体指定组件的每个部分:

library(lubridate)

bookings %>%
    mutate(date_time = update(start_time,
                              year = year(booking_date),
                              month = month(booking_date),
                              day  = day(booking_date)))

  booking_date        start_time          date_time          
  <dttm>              <dttm>              <dttm>             
1 2019-02-09 00:00:00 1899-12-31 08:45:00 2019-02-09 08:45:00
2 2019-02-16 00:00:00 1899-12-31 08:45:00 2019-02-16 08:45:00
3 2019-02-23 00:00:00 1899-12-31 08:45:00 2019-02-23 08:45:00
4 2019-03-02 00:00:00 1899-12-31 08:45:00 2019-03-02 08:45:00
5 2019-03-09 00:00:00 1899-12-31 08:45:00 2019-03-09 08:45:00
6 2019-03-16 00:00:00 1899-12-31 08:45:00 2019-03-16 08:45:00