这是我拥有的数据框的子集:
structure(list(name = c("waldorf", "waldorf", "waldorf", "waldorf",
"waldorf", "waldorf", "waldorf", "waldorf", "waldorf", "waldorf",
"waldorf", "waldorf", "waldorf", "waldorf", "waldorf", "waldorf",
"waldorf", "waldorf", "waldorf", "waldorf", "waldorf", "waldorf",
"waldorf", "waldorf"), date = structure(c(1559347200, 1559347200,
1559347200, 1559347200, 1559347200, 1559347200, 1559347200, 1559347200,
1559347200, 1559347200, 1559347200, 1559347200, 1559347200, 1559347200,
1559347200, 1559347200, 1559347200, 1559347200, 1559347200, 1559347200,
1559347200, 1559347200, 1559347200, 1559347200), tzone = "UTC", class = c("POSIXct",
"POSIXt")), time = structure(c(0, 3600, 7200, 10800, 14400, 18000,
21600, 25200, 28800, 32400, 36000, 39600, 43200, 46800, 50400,
54000, 57600, 61200, 64800, 68400, 72000, 75600, 79200, 82800
), class = c("hms", "difftime"), units = "secs"), datetime = structure(c(1559347200,
1559350800, 1559354400, 1559358000, 1559361600, 1559365200, 1559368800,
1559372400, 1559376000, 1559379600, 1559383200, 1559386800, 1559390400,
1559394000, 1559397600, 1559401200, 1559404800, 1559408400, 1559412000,
1559415600, 1559419200, 1559422800, 1559426400, 1559430000), tzone = "UTC", class = c("POSIXct",
"POSIXt")), temp = c(72.9, 70.6, 69.7, 69.6, 68, 68.1, 67.9,
67.8, 69.6, 71.9, 75.6, 78.3, 80.1, 80.8, 82.8, 83.5, 83.3, 82.9,
81.5, 79.4, 77.6, 72.4, 70.6, 69.8)), row.names = c(NA, -24L), class = c("tbl_df",
"tbl", "data.frame"))
扩展的数据框包含 5 年天数的每小时温度数据。如何使用日期时间或其他列来计算数据框中每天下午 1 点到下午 5 点的平均温度?我可以在 dplyr 字符串中执行此操作吗?
这里有两种变体:
library(dplyr); library(lubridate)
df |>
filter(hour(datetime) %in% 13:16) |>
summarize(avg_temp = mean(temp), .by = date)
df |>
mutate(avg_temp = mean(temp[hour(datetime) %in% 13:16]), .by = date)