How to input monthly data for time series plot in R?

215 views Asked by At

I have data stored in .csv file which has two columns:

Date    mean
201001    234
201002    167
...
...
...
202005    289

(format is yyyymm)

I want to create a time series plot for the same. I am trying to do so using the following code in R:

library("zoo")
library("dplyr")
library("ggplot2")
library("ggfortify") 
alb_mean <- read.csv("D:/Monthtest/alb_mean.csv")
View(alb_mean)
ff <- zoo(alb_mean$mean, seq(from=as.Date("201001"), to=as.Date("202005", by=1)))

It gives me following error: Error in charToDate(x) : character string is not in a standard unambiguous format

Why it is giving me this error? Do I need to change the date format? Kindly help.

2

There are 2 answers

3
G. Grothendieck On BEST ANSWER

csv means comma separated values but there are no commas in the data shown in the question so we will create an actual csv file in the Note at the end and use that. First read it into a zoo object using read.csv.zoo -- if the data is in the format shown in the question as opposed to csv then use read.zoo(text = "lab_mean.csv", header = TRUE, FUN = to.ym) instead. In either case convert the first column to yearmon class which represents a year and month without a day. Then plot it using autoplot. See ?autoplot.zoo for further customizations.

library(ggplot2)
library(zoo)

to.ym <- function(x) as.yearmon(as.character(x), "%Y%m")
alb_mean <- read.csv.zoo("alb_mean.csv", FUN = to.ym)
autoplot(alb_mean)

screenshot

## Note

Lines <- "Date    mean
201001    234
201002    167
202005    289"
Lines |>
  read.table(text = _, header = TRUE) |>
  write.csv("alb_mean.csv", row.names = FALSE, quote = FALSE)
2
SamR On

Your Date column is not a Date object. as.Date("201001") produces an error because R does not know what the format is. Your sequence should be:

ff <- zoo(
    alb_mean$mean, 
    seq(from = as.Date("2010-01-01"), to = as.Date("2020-05-01"), by = "1 month")
)

However, this will only assign the correct values to each mean if your original data has no missing months and is in date order. It is safer to use the values from the Date column, appending 01 to put them in "%Y%m%d" format:

ff  <- zoo(
    alb_mean$mean, 
    as.Date(paste0(alb_mean$Date, "01"), format = "%Y%m%d")    
)