我有以下数据框,其中显示了两个参数的每小时模拟重金属浓度:
Date<-c("2013-01-01 02:00:00","2013-01-01 03:00:00","2013-01-02 02:00:00","2013-01-02 03:00:00","2013-01-01 02:00:00","2013-01-01 03:00:00","2013-01-02 02:00:00","2013-01-02 03:00:00")
Parameter<-c("Par1","Par1","Par1","Par1","Par2","Par2","Par2","Par2")
sim1<-c(1,4,3,2,6,5,3,5)
sim2<-c(3,2,3,1,8,2,7,3)
obs<-data.frame(Date,Parameter,sim1,sim2)
obs$Date<-as.POSIXct(obs$Date)
我需要每个参数的每日平均值。有任何想法吗?我尝试aggregate
,但无法弄清楚如何按参数和日期分组。
答案 0 :(得分:2)
我们可以使用Date
将'Date'转换为as.Date
类,在group_by中将其与'Parameter'一起使用,并使用{获得其余列的mean
{1}}
summarise_all
或使用library(tidyverse)
obs %>%
group_by(Daily = as.Date(Date), Parameter) %>%
summarise_all(mean)
中的aggregate
base R
或使用aggregate(.~ Date + Parameter, transform(obs, Date = as.Date(Date)), mean)
by