In base R, you can use tapply
or aggregate
to compute group means.
First, some fake data, since you have posted none.
The time
variable is created with seq.POSIXt
, starts today minus 1 day (Sys.Date - 1
) and end today (Sys.time
) by increments of 30 seconds.
The numeric variable is just a gaussian random variable.
set.seed(3224)
time <- seq(as.POSIXct(Sys.Date()) - 3, Sys.time(), by = "30 secs")
dat <- data.frame(time, m_Pm = cumsum(rnorm(length(time))))
Now, start by getting just the hours, then find group means.
dat$datehour <- format(dat$time, "%Y-%m-%d %H")
tapply(dat$m_Pm, dat$datehour, mean)
#2018-08-12 00 2018-08-12 01 2018-08-12 02 2018-08-12 03 2018-08-12 04
# 0.5159274 8.2868756 24.8056718 26.4107549 27.6064418
#2018-08-12 05 2018-08-12 06 2018-08-12 07 2018-08-12 08
# 33.0096640 42.7479312 40.2468852 41.5313523
agg <- aggregate(m_Pm ~ datehour, dat, mean)
head(agg)
# datehour m_Pm
#1 2018-08-12 00 0.5159274
#2 2018-08-12 01 8.2868756
#3 2018-08-12 02 24.8056718
#4 2018-08-12 03 26.4107549
#5 2018-08-12 04 27.6064418
#6 2018-08-12 05 33.0096640
As for the graph, I will draw a line graph using package `ggplot2`. The x axis is formated with package `scales` function `scale_x_datetime`.
library(ggplot2)
library(scales)
ggplot(agg, aes(x = as.POSIXct(datehour, format = "%Y-%m-%d %H"), y = m_Pm)) +
geom_point() +
geom_line() +
labs(x = "Hour", y = "Mean value") +
scale_x_datetime(labels = date_format("%Y-%m-%d %H"))
