【问题标题】:Separate date and time into two columns in R and summarize the rows based on time of the day在 R 中将日期和时间分成两列,并根据一天中的时间汇总行
【发布时间】:2020-10-18 13:51:25
【问题描述】:

如何将日期和时间分成两列?然后汇总所有这些行并取一天中每 12 小时的平均值? (基本上每天只有两卷数据,而不是49卷,每卷是一天1/2小时的平均温度。)请看下图,非常感谢您的帮助!

当前代码:

test <- weather %>% 
  unite(date, Year.Month.Day.Hour.Minutes.in.YYYY, MM, DD, sep="-") %>% 
  unite(time, HH24, MI.format.in.Local.time, sep=":") %>% 
  mutate(timestamp=paste(date, time) %>% 
           as.POSIXct(., format="%Y-%m-%d %H:%M")) %>%
  select(timestamp, temperature)

电流输出:

预期输出:

【问题讨论】:

  • 请以可复制的形式添加我们可以复制和使用的数据。图片无济于事

标签: r dplyr tidyverse tidyr


【解决方案1】:

这样的?

weather %>% 
  unite(date, Year.Month.Day.Hour.Minutes.in.YYYY, MM, DD, sep="-") %>% 
  unite(time, HH24, MI.format.in.Local.time, sep=":") %>% 
  mutate(timestamp=paste(date, time) %>% 
           as.POSIXct(., format="%Y-%m-%d %H:%M")) %>%
  select(timestamp, temperature) %>% 
  group_by(
    Date = as.Date(timestamp), 
    Time = if_else(lubridate::am(timestamp), "Morning", "Afternoon")
  ) %>% 
  summarise(
    `Average Temperature` = mean(Air.Temperature.in.degrees.C)
  )

【讨论】:

    猜你喜欢
    • 2015-06-03
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 2019-10-29
    • 2021-02-04
    • 2016-09-12
    • 2019-09-23
    • 1970-01-01
    相关资源
    最近更新 更多