Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

Say I have an excel file with format like this (to download from this link):

enter image description here

Note the first column is year and the first row is month.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I try to convert it to time series object then draw a seasonal plot using ggseasonplot or ggplot2.

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)

Output:

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

How could I do that correctly using R? Thanks in advance.

References:

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

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

>Solution :

If it is a continuous time series, then you can drop the month column and put all years into one column (and also remove the year after using melt). Then, you can just specify your start year and month.

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

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading