Reading month-year from a csv file and convert it into a time series

I have a csv file which contains two columns, time and value. The format of the time column like this "yyyy-mm". For example: '2012-10'. I need to read the file from a shiny app and need to convert it in a time series. But I find difficulties converting the time format into time series . Can anyone please help me regarding this issue?

Thanks in advance.

*I am very new in this shiny sector.

Here is the short snippet of my csv file:
date

Does the value in the Value column represent the day, i.e. does combining the Date and Value column row-wise represent a date?

Not Actually. The file contains the monthly number of customers of a company. So the Value column represents the number of customers here.

Here is one solution.

DF = data.frame(Date=c("2012-02","2012-03","2012-04"),
                Value=c(18,25,31))
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
DF$Date <- ym(DF$Date)
DF
#>         Date Value
#> 1 2012-02-01    18
#> 2 2012-03-01    25
#> 3 2012-04-01    31

Created on 2022-07-21 by the reprex package (v2.0.1)

1 Like

Thank you so much @FJCC for the reply. Can you help me with an another issue ? I have a file with a irregular date format . I am giving a picture with the date format. How can I convert this to a time series?

Thank in advance again.
date

The lubridate package has many convenient functions for changing text to dates. It looks like your dates are in a month/day/year format, so you can use the mdy() function in the same way that I used the ym() function above.

1 Like

Hello @FJCC I am here with another problem. Sorry for bothering you so much. Actually I have to optimize a server.R code. Lets see the code first.

library(shiny)
library(tsbox)
library(fpp2)
library(xts)
library(zoo)
library(lubridate)
library(gdata)

shinyServer(function(input,output,session){
  
  tsData <- reactive({
    validate(
      need(input$file != "", "Please select a data set")
    )
    
    file_in <- input$file
    x <- file_in$datapath
    datf = read.csv(x,header = TRUE, stringsAsFactors = FALSE)
    datf[,1] = as.character(datf[,1])  
    datf[,2] <- as.numeric(datf[,2])
    
    if (sum(is.na(dmy(datf[,1]))) == 0) datf[,1] = dmy(datf[,1])
    else if (sum(is.na(mdy(datf[,1]))) == 0) datf[,1] = mdy(datf[,1])
    else if (sum(is.na(ymd(datf[,1]))) == 0) datf[,1] = ymd(datf[,1])
    else if (sum(is.na(my(datf[,1]))) == 0)  datf[,1] =  my(datf[,1])
    else if (sum(is.na(ym(datf[,1]))) == 0)  datf[,1] =  ym(datf[,1])
    else if (sum(is.na(dm(datf[,1]))) == 0)  datf[,1] =  dm(datf[,1])
    else if (sum(is.na(md(datf[,1]))) == 0)  datf[,1] =  md(datf[,1])
    
    datfXts<- xts(datf[,2], order.by = datf[,1])
    datf_ts <- ts_ts(datfXts)
    return(datf_ts)
  })
  columnNames<-reactive({
    file_in <- input$file
    columns <- colnames(read.csv(file_in$datapath,header = TRUE, stringsAsFactors = FALSE))
    if(startsWith(columns[1], 'X', trim=TRUE) && startsWith(columns[2], 'X', trim=TRUE)  ){
      
      columns[1] = "Time"
      columns[2] = "Value"
    }
    
    return(columns)
  })
  
  output$plot1 <- renderPlot({
    datf_ts<-tsData()
    columns <- columnNames()
    autoplot(datf_ts, series = "Original Data") + xlab(columns[1]) + ylab(columns[2])
  })
  
  output$plot2 <- renderPlot({
    datf_ts<-tsData()
    datf_ts %>% auto.arima() %>% checkresiduals()
  })
  
  output$plot3 <- renderPlot({
    datf_ts<-tsData()
    columns <- columnNames()
    datf_ts %>% auto.arima() %>% fitted() %>% autoplot(series = "Fitted Data") + autolayer(datf_ts, series = "Original Data") + xlab(columns[1]) + ylab(columns[2])
  })
  
  output$plot4 <- renderPlot({
    datf_ts<-tsData()
    columns <- columnNames()
    datf_ts %>% auto.arima() %>% forecast() %>% autoplot(series = "Origina + Forecast") + xlab(columns[1]) + ylab(columns[2])
  })
})

Actually the reactive functions are called four times after uploading every file. It should be only one call . I am having error with global variables. How can I do it?

Thanks in advance.

It is fine to ask more questions. That is what the forum is for. Since your latest question is entirely unrelated to the original question of this thread, please start another thread. I am not very knowledgeable about shiny, so I probably will not be able to solve your problem, but there are many other people here who probably can.

1 Like

Thank you for your suggestion @FJCC

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.