【问题标题】:How to extract time from datetime and plot histogram如何从日期时间中提取时间并绘制直方图
【发布时间】:2021-10-15 04:18:17
【问题描述】:

我有一个数据,其中包含某事件发生多年的日期时间。

我创建了一个带有日期时间的小示例 tibble。有人可以帮我解决以下问题吗-

  1. 时间是 UTC,如何转换为 EST

  2. 如何绘制 (ggplot) 随时间发生的直方图。

library(tidyverse)

tbl <- tibble(datetime = c("2019-06-17 21:55:00 UTC", "2019-06-06 21:50:00 UTC", "2018-09-20 11:14:00 UTC", "2019-02-07 16:12:00 UTC",
"2020-01-30 16:54:00 UTC", "2019-06-03 21:26:00 UTC", "2018-08-17 13:03:00 UTC", "2019-09-06 18:55:00 UTC",
"2019-03-28 10:30:00 UTC", "2019-11-21 20:57:00 UTC", "2019-05-29 16:47:00 UTC", "2019-11-06 00:32:00 UTC",
"2018-04-26 20:18:00 UTC", "2019-09-11 19:21:00 UTC", "2019-10-04 13:24:00 UTC", "2018-12-11 23:39:00 UTC",
"2019-10-03 19:15:00 UTC", "2020-11-11 22:11:00 UTC", "2020-12-17 23:11:00 UTC", "2019-07-25 18:20:00 UTC"))

【问题讨论】:

    标签: r ggplot2 tidyverse lubridate


    【解决方案1】:

    下面会将小标题转换为带有 EST 时间的数据框,然后创建一个直方图。

    library(tidyverse)
    
    tbl <- tibble(datetime = c("2019-06-17 21:55:00 UTC", "2019-06-06 21:50:00 UTC", "2018-09-20 11:14:00 UTC", "2019-02-07 16:12:00 UTC",
                               "2020-01-30 16:54:00 UTC", "2019-06-03 21:26:00 UTC", "2018-08-17 13:03:00 UTC", "2019-09-06 18:55:00 UTC",
                               "2019-03-28 10:30:00 UTC", "2019-11-21 20:57:00 UTC", "2019-05-29 16:47:00 UTC", "2019-11-06 00:32:00 UTC",
                               "2018-04-26 20:18:00 UTC", "2019-09-11 19:21:00 UTC", "2019-10-04 13:24:00 UTC", "2018-12-11 23:39:00 UTC",
                               "2019-10-03 19:15:00 UTC", "2020-11-11 22:11:00 UTC", "2020-12-17 23:11:00 UTC", "2019-07-25 18:20:00 UTC"))
    
    time <- as.POSIXct(tbl$datetime, "%F %T", tz="UTC")
    
    time <- data.frame(datetime=strptime(format(time,"%T", tz="America/New_York"), "%H:%M:%S"))
    
    
    ggplot(data=time, aes(x=datetime)) +
      stat_bin(bins = 24)
      geom_histogram()
    

    【讨论】:

    • 非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 2019-05-08
    • 2019-12-14
    • 1970-01-01
    相关资源
    最近更新 更多