Mean of a tsibble

33 Views Asked by At

I want to compute the mean of each series within a given tsibble. This is my code:

library(tsibble)

data <- tibble(
  Time = seq(as.Date("2020-01-01"), by = "1 months", length.out = 4),
  Series1 = c(3, 4, 1, 2), Series2 = c(0, 1, 2, 3)
)
ts <- as_tsibble(data, index = Time)

print(ts)
print(colMeans(ts))

It gives me

Error in colMeans(ts) : 'x' must be numeric

(This is the same error as with colMeans(data).)

Why doesn't tsibble overload colMeans to ignore the date index?
What else can I use?

(This is with R 4.0.4, tsibble 1.1.4.)

1

There are 1 best solutions below

1
Robert Pollak On BEST ANSWER

My own answers:

  • tsibble does not overload colMeans, because it is generally not useful to compute overall means of time series.

  • Instead, I can convert to tibble (in case data is not available any more), remove the time index, then call colMeans:

    library(dplyr)
    ts_data <- select(tibble(ts), -Time)
    print(colMeans(ts_data))