Yang Kewen
Yang Kewen

Reputation: 25

How to group by time for XTS object in R?

I have this a time series xts object in R.

Basically the time series duration is a few months, I want to know the trend for different time points.

I want to get the median or mean for different time points.

library(xts)
library(lubridate)
Time <- seq(ymd_hms("2019-01-01 00:00:00"), ymd_hms("2019-03-29 23:59:59"), "hour")
length(Time)
Data <- rnorm(2112, 1, 5)
Time_Series <- xts(x = Data , order.by = Time)

Take this code as example.

How can I get the mean for the data at time 00:00:00? similarly the mean of data in 01:00:00, 02:00:00, 03:00:00 ...

Thank you for your helping in advance!

Upvotes: 1

Views: 921

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 269596

This one-liner uses aggregate.zoo producing a zoo object whose time is the hour. No additional packages are used.

aggregate(Time_Series, hour, mean)

giving:

0  0.4237426
1  1.8814963
2  1.2917437
3  1.4307028
4  1.3691019
5  0.3762082
6  1.3866948
# ...snip...

Note that the data in the question is not reproducible since set.seed was not used so this just shows what the output looks like.

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388982

One way would be to convert the time series to dataframe and get mean by hour.

library(dplyr)
library(lubridate)

Time_Series %>%
  zoo::fortify.zoo() %>%
  group_by(hour = hour(Index)) %>%
  summarise(mn = mean(Data))

# A tibble: 24 x 2
#    hour     mn
#   <int>  <dbl>
# 1     0  1.53 
# 2     1  0.414
# 3     2  1.24 
# 4     3  1.07 
# 5     4  1.32 
# 6     5  1.34 
# 7     6  0.998
# 8     7 -0.615
# 9     8  0.924
#10     9  0.484
# … with 14 more rows

A method with base R would be using aggregate

df <- zoo::fortify.zoo(Time_Series)
df$hour <- format(df$Index, "%H")
aggregate(Data~hour, df, mean)

data

set.seed(23)
Time <- seq(ymd_hms("2019-01-01 00:00:00"), ymd_hms("2019-03-29 23:59:59"), "hour")
Data <- rnorm(2112, 1, 5)
Time_Series <- xts(x = Data , order.by = Time)
names(Time_Series) <- "Data"

Upvotes: 0

Related Questions