Time series in 'R'




A data set contains timestamp with values like ‘23-1-2007’ and sales value and so on with other variables. The values for timestamp variable will be on daily basis or in between breaks. How can i read this data and create an object that is being read by “ts” command in “R”. Do I need to use dply / dplyr packages to aggregate the data in a form which will be readable by “ts” command?

Please suggest.
I want to use this data to forecast a sales transactions weekly, quarterly, yearly etc…

Thanks in advance.


Hi @manalykum
From what i understand, you will have broken day level data and you want to read it into the time series object

I tried something here

data1 <- data.frame(date=c('23-01-2007','23-02-2007','15-03-2007'),sales=c(100,200,300),country=c('A','B','C'))
#Now what you can do is, insert dummy time entries. This can be daily/monthly/quarterly/yearly
maxDate <- max(as.Date(data1$date,format="%d-%m-%Y"))
minDate <- min(as.Date(data1$date,format="%d-%m-%Y"))
newdata1 <- data.frame(date=seq(minDate,maxDate,by="day"))
newdata1$date <- format(newdata1$date,"%d-%m-%Y")
newdata1 <- merge(newdata1,data1,by='date',all.x=TRUE)
# Based on what is your periodicity, you have to select the frequency. 365 for daily, 12 for monthly, 1 for yearly etc etc
myts <- ts(newdata1[,c(2,3)],frequency=365,start=c(2007,23))

Let me know if this helped you in any capacity