0
votes

I have a following dataframe in R

   Date           Value
   1986-01-02     25.67
   1986-01-03     23.56
   1986-01-06     34.56
   1986-01-07     23.77
   1986-01-08     25.67
   1986-01-09     26.56
   1986-01-10     25.56
   1986-01-13     28.77
   . 
   .
   .
   2018-07-03     73.45
   2018-07-04     74.34
   2018-07-05     73.45
   2018-07-06     74.34
   2018-07-09     72.34

Date column is in POSIXct format and excluding weekends (Saturday and Sunday).I want to convert it into a daily time series in R.

I am doing following

 ts_object <- ts(df,frequency = 365)

It gives me following ts

 Time Series:
 Start = c(1, 1) 
 End = c(23, 193) 
 Frequency = 365 
            Date         Value
  1.000000  505008000    25.67
  1.002740  505094400    23.56
  1.005479  505353600    34.56 

Why its not taking date in correct format. Am I setting the frequency right for daily time series object?

1

1 Answers

0
votes

You will have to add missing data (Saturday and Sunday) because frequency = 365 doesn't exclude the missing data.

One way to generate that is as follows

df <- data.frame( Date = seq(as.Date("1986-01-02"), as.Date("1986-01-07"), 1))
df$Date <- as.character(df$Date)

ds <- read.table(text = "Date           Value
1986-01-02     25.67
1986-01-03     23.56
1986-01-06     34.56
1986-01-07     23.77", header = T)

df <- merge(df, ds, by = "Date", all.x = T)

df[is.na(df)] <- 0

df

        Date Value
1 1986-01-02 25.67
2 1986-01-03 23.56
3 1986-01-04  0.00
4 1986-01-05  0.00
5 1986-01-06 34.56
6 1986-01-07 23.77