How do I create daily time series starting from a specific date
Asked Answered
B

4

7

I am trying to create a ts object using R for a daily time series that starts on 24.02.2015 and ends on 13.04.2015. I have put the frequency=7 for daily data but I cannot find a way to put the exact date as start argument.

Brassiere answered 18/7, 2015 at 12:27 Comment(2)
ts is not normally used for dates. Use zoo or xts instead.Christie
possible duplicate of Creating a Unique Sequence of DatesBellarmine
L
14

I think this is what you want, using the decimal_date() function from 'lubridate' to get the proper start time for a daily series and assuming that the vector of values you want to index as a ts is called x and is of the proper length:

library(lubridate)
df <- ts(x, start = decimal_date(as.Date("2015-02-24")), frequency = 365)

Here's what that looks like if I use rnorm() to generate an x of the proper length:

> df
Time Series:
Start = c(2015, 55) 
End = c(2015, 103) 
Frequency = 365 
[1]  0.4284579  1.9384426  0.1242242 -2.4002789 -0.4064669  0.6945274 -0.5172909  0.4772347  0.8758635 -1.7233406  0.5929249  1.5662611  1.0692173 -0.1354226
[15]  1.1404375  0.7714662 -0.2871663 -5.2720038 -1.7353146 -0.7053329  1.0206803  1.7170262 -0.3469172  0.2594851  2.0371700 -2.1549066 -0.6639050 -0.4912258
[29] -0.3849884 -3.0448583 -1.3317834  1.6173705  0.7176759 -0.8646802 -1.7697016  1.1114061  0.6941131 -0.1942612 -0.1836107 -0.5850649 -1.7449090 -3.3646555
[43] -0.4341833  1.9721407  1.4995265  1.7168002  1.8617295 -3.4578959  1.1639413

Note that for daily indexing, you want frequency = 365, not 7, which denotes weekly indexing.

If you want a vector of dates that you can use in 'zoo' instead, this does it:

seq(from = as.Date("2015-02-24"), to = as.Date("2015-04-13"), by = 1)

So you would create a zoo object like this:

zoo(x, seq(from = as.Date("2015-02-24"), to = as.Date("2015-04-13"), by = 1))
Ligni answered 18/7, 2015 at 12:33 Comment(1)
For longer time series, how are leap years handled?Wait
A
1

And if you want a table with date column, you can use:

df <- data.frame(date = seq(from = as.Date("2015-02-24"), to = as.Date("2015-04-13"), by = 1))
Anya answered 17/7, 2018 at 8:10 Comment(0)
L
0

Using the xts library:

library(xts)
data_xts <- xts(x=dataframe$x, order.by=as.Date(dataframe$date, "%m/%d/%Y"))

With this method, you can't or don't have to specify the end date.

The output looks like this:

              [,1]
2020-01-01  7168.3
2020-01-02  7174.4
2020-01-03  6942.3
2020-01-04  7334.8
Lazybones answered 8/12, 2020 at 3:40 Comment(0)
D
0

If you put frequency = 7, it means that series samples 7 days between units and therefore your unit is week. Then, the ts object for the period you mention will be defined as follows:

start_dt <- as.Date("24.02.2015", "%d.%m.%Y")
end_dt <- as.Date("13.04.2015", "%d.%m.%Y")

# number of days
as.numeric(end_dt - start_dt + 1)
#> [1] 49

# Create a ficticious time series
data <- 1:49

# find the week and day that coresponds to start_dt
strftime(start_dt, "isoweek: %V, weekday: %u (%A)")
#> [1] "isoweek: 09, weekday: 2 (martes)"

ts_data <- ts(data, start = c(9, 2), frequency = 7)
ts_data
#> Time Series:
#> Start = c(9, 2) 
#> End = c(16, 1) 
#> Frequency = 7 
#>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#> [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

Note that the cycle function will be for a week period (frequency = 7) and identify each data point as the day of the week.

cycle(ts_data)
#> Time Series:
#> Start = c(9, 2) 
#> End = c(16, 1) 
#> Frequency = 7 
#>  [1] 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4
#> [39] 5 6 7 1 2 3 4 5 6 7 1

Also, you can look at the answer for a similar question: https://mcmap.net/q/1477824/-creating-time-series-for-data-sampled-daily-in-r

Created on 2023-03-05 with reprex v2.0.2

Dysgenics answered 5/3, 2023 at 16:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.