答案 0 :(得分:1)
我们可以按数据集的implementation 'androidx.coordinatorlayout:coordinatorlayout:1.0.0'
分组,在index
中使用aggregate
中的值,以获取base R
mean
NB:在这里,我们假设数据集的格式为aggregate(dat, index(dat), FUN = mean)
或xts
。如果数据集包含月份列,则使用
zoo
答案 1 :(得分:1)
library(tidyverse)
library(lubridate)
#example data from airquality:
aq<-as_data_frame(airquality)
aq$mydate<-lubridate::ymd(paste0(2018, "-", aq$Month, "-", aq$Day))
> aq # A tibble: 153 x 7 Ozone Solar.R Wind Temp Month Day mydate <int> <int> <dbl> <int> <int> <int> <date> 1 41 190 7.40 67 5 1 2018-05-01 2 36 118 8.00 72 5 2 2018-05-02 3 12 149 12.6 74 5 3 2018-05-03
aq %>%
group_by("Month" = month(mydate)) %>%
summarize("Mean_Temp" = mean(Temp, na.rm=TRUE))
Summarize可以返回多个summary functions:
aq %>%
group_by("Month" = month(mydate)) %>%
summarize("Mean_Temp" = mean(Temp, na.rm=TRUE),
"Num" = n(),
"SD" = sd(Temp, na.rm=TRUE))
# A tibble: 5 x 4 Month Mean_Temp Num SD <dbl> <dbl> <int> <dbl> 1 5.00 65.5 31 6.85 2 6.00 79.1 30 6.60 3 7.00 83.9 31 4.32 4 8.00 84.0 31 6.59 5 9.00 76.9 30 8.36
答案 2 :(得分:1)
使用aggregate()
函数的另一种方式。
> aggregate(Temp ~ Month, data=airquality, FUN = mean) Month Temp 1 5 65.54839 2 6 79.10000 3 7 83.90323 4 8 83.96774 5 9 76.90000
答案 3 :(得分:1)
data.table答案:
# load libraries
library(data.table)
library(lubridate)
setDT(dt)
dt[, .(meanValue = mean(value, na.rm =TRUE)), by = .(monthDate = floor_date(dates, "month"))]
dt
至少具有列value
和dates
的地方。