我有一個像這樣的簡單資料框:
dput(a)
structure(list(date = structure(c(1640995200, 1641081600, 1641168000,
1641254400, 1641340800, 1641427200, 1643673600, 1643760000, 1643846400,
1643932800, 1644019200, 1644105600, 1646092800, 1646179200, 1646265600,
1646352000, 1646438400, 1646524800, 1646611200, 1646697600), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), value = c(18, 27, 28, 50, 42, 18,
18, 39, 16, 49, 42, 26, 18, 38, 15, 50, 18, 10, 36, 22)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -20L))
值列用于每日日期。如何獲得該列的月 平均值value?
uj5u.com熱心網友回復:
我們使用 將format日期修改為年月日為 1 并得到mean
library(dplyr)
a %>%
group_by(month = format(date, '%Y/%m/01')) %>%
summarise(value = mean(value, na.rm = TRUE))
-輸出
# A tibble: 3 × 2
month value
<chr> <dbl>
1 2022/01/01 30.5
2 2022/02/01 31.7
3 2022/03/01 25.9
如果是年月,則更format改為format(date, '%Y-%b')
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/521633.html
標籤:rdplyr
上一篇:按縣合并遷入遷出資料
