使用R将行名为月,列名为年的数据框转换为时间序列对象

Convert a dataframe whose row name is month and column name is year to a time series object using R

假设我有一个格式如下的 excel 文件(从 this link 下载):

注意第一列是年份,第一行是月份。

我尝试将其转换为时间序列对象,然后使用 ggseasonplotggplot2 绘制季节性图。

df <- openxlsx::read.xlsx('dataset1.xlsx', sheet='Sheet1', colNames=TRUE, rowNames = TRUE)
# df <- t(df)
df <- ts(df, start = c(2008, 1), end=c(2021, 12), frequency = 12)
forecast::ggseasonplot(df, col=rainbow(12), year.labels=TRUE)

输出:

Error in data.frame(y = as.numeric(x), year = trunc(round(time(x), 8)),  : 
  arguments imply differing number of rows: 2352, 168

我如何使用 R 正确地做到这一点?提前致谢。

参考文献:

https://pkg.robjhyndman.com/forecast/reference/seasonplot.html

https://afit-r.github.io/ts_exploration

如果是连续的时间序列,那么可以去掉month这一列,把所有的年份都放在一列中(使用melt之后的年份也去掉)。然后,您只需指定您的开始年份和月份即可。

output <- ts(reshape::melt(df[,-1])[,2], start = c(2008, 1), frequency = 12)
forecast::ggseasonplot(output, col=rainbow(12), year.labels=TRUE)

数据

df <- structure(list(month = 1:12, `2008` = c(4466.7095, 3654.5805, 
10195.65, 10093.13, 11854.13, 18171.78, 13724.1, 12759.61, 14951.02, 
13318.36, 14425.07, 20553.11), `2009` = c(4597.063947, 5678.726053, 
13286.21, 13520.3, 16438.02, 24578.03, 17833.66, 17052.78, 20191.81, 
17533.16, 17924.44, 25504.42), `2010` = c(7034.610811, 5979.419189, 
16778.65, 16950.07, 20615.55, 30689.08, 21818.87, 21131.49, 24871.84, 
21686.52, 23141.76, 30717.07)), class = "data.frame", row.names = c(NA, 
-12L))