[R] Calculate daily means from 5-minute interval data
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sun Aug 29 19:01:02 CEST 2021
Hello,
You have date and hour in two separate columns, so to compute daily
stats part of the work is already done. (Were they in the same column
you would have to extract the date only.)
# convert to class "Date"
df1$date <- as.Date(df1$date)
# function to compute the stats required
# it's important to note that all the stats
# are returned in a vector, see below
fun <- function(x, na.rm = FALSE){
c(mean_cfs = mean(x, na.rm = na.rm),
sd_cfs = sd(x, na.rm = na.rm))
}
# now this will put a *matrix* under cfs
# each row has the statistics computed
# by the function
agg <- aggregate(cfs ~ date, df1, fun)
str(agg)
#'data.frame': 1 obs. of 2 variables:
# $ date: Date, format: "2020-08-26"
# $ cfs : num [1, 1:2] 110400 16143
# ..- attr(*, "dimnames")=List of 2
# .. ..$ : NULL
# .. ..$ : chr [1:2] "mean_cfs" "sd_cfs"
# so now put everything in separate columns
agg <- cbind(agg[-ncol(agg)], agg[[ncol(agg)]])
str(agg)
#'data.frame': 1 obs. of 3 variables:
# $ date : Date, format: "2020-08-26"
# $ mean_cfs: num 110400
# $ sd_cfs : num 16143
Hope this helps,
Rui Barradas
Às 17:49 de 29/08/21, Rich Shepard escreveu:
> On Sun, 29 Aug 2021, Eric Berger wrote:
>
>> Provide dummy data (e.g. 5-10 lines), say like the contents of a csv
>> file,
>> and calculate by hand what you'd like to see in the plot. (And describe
>> what the plot would look like.)
>
> Eric,
>
> Mea culpa! I extracted a set of sample data and forgot to include it in the
> message. Here it is:
>
> date,time,cfs
> 2020-08-26,09:30,136000
> 2020-08-26,09:35,126000
> 2020-08-26,09:40,130000
> 2020-08-26,09:45,128000
> 2020-08-26,09:50,126000
> 2020-08-26,09:55,125000
> 2020-08-26,10:00,121000
> 2020-08-26,10:05,117000
> 2020-08-26,10:10,120000
> ...
> 2020-08-26,23:10,108000
> 2020-08-26,23:15,96200
> 2020-08-26,23:20,86700
> 2020-08-26,23:25,103000
> 2020-08-26,23:30,103000
> 2020-08-26,23:35,99500
> 2020-08-26,23:40,85200
> 2020-08-26,23:45,103000
> 2020-08-26,23:50,95800
> 2020-08-26,23:55,88200
>
> Rich
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list