- Update(2021/5/13, 11:55AM): The data is not reproducible. Thank you @G. Grothendieck for giving me a comment.
(This is my first post in Stackoverflow, so please let me know if there's any basic mistakes I made in this question)
Hi,
I have a hourly-measured data of air quality over multiple days in R, and I would like to calculate the average air quality at specific time periods.
Here's a subset of my reproducible data. It is in xts format.
# Make a structure of data
dput(Air_sample[1:6,1:1])
# Create a data from the structure above.
Air <- structure(
c(2.6, 2, 2.2, 2.2, 1.6, 1.2),
class = c("xts", "zoo"),
index = structure(
c(
1078959600,
1078963200,
1079046000,
1079049600,
1079132400,
1079136000
),
tzone = "",
tclass = c("POSIXct",
"POSIXt")
),
.Dim = c(6L, 1L),
.Dimnames = list(NULL, c("True.CO")))
> Air
True.CO
2004-03-10 18:00:00 2.6
2004-03-10 19:00:00 2.0
2004-03-11 18:00:00 2.2
2004-03-11 19:00:00 2.2
2004-03-12 18:00:00 1.6
2004-03-12 19:00:00 1.2
I want to calculate average CO at specific time (ex. 6PM) from multiple days. So the result would be something like below.
Air_average <- data.frame("Time" = c("18:00","19:00"), "Average CO" = c(2.1333,1.8))
> Air_average
Time Average.CO
1 18:00 2.1333
2 19:00 1.8000
I tried different function via Googling such as "period.apply", "subset", "window", etc. But none of them seems to work.
Is there any way to do this?
Thank you.