【问题标题】:Trying to combine dates and times尝试结合日期和时间
【发布时间】:2019-04-25 22:58:54
【问题描述】:

我正在尝试结合日期和时间。这些是从文件中导入的,如下所示:

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

谢谢。

【问题讨论】:

    标签: r dplyr tidyverse lubridate stringr


    【解决方案1】:

    如果您想从booking_date 获取start_time 的日期,基本的R 方法是从paste 获取booking_date 的“日期”部分和start_time 的“时间”部分并将它们转换为@987654326 @。

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

    【讨论】:

      【解决方案2】:

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

      date() &lt;- 允许您设置日期/时间对象的日期组件:

      # 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
      

      由于它使用赋值 (&lt;-),因此您不能将第一种方法用作管道的一部分。在管道中起作用的是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
      

      【讨论】:

      • 有没有办法通过管道而不是使用$
      猜你喜欢
      • 1970-01-01
      • 2022-12-10
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 2013-12-25
      • 1970-01-01
      相关资源
      最近更新 更多