5

I try to convert a daily dataset to ts, but how do you deal with leap year? so what value should I set the frequency equal to?

ts(data,start=c(2010,1,1),frequency=365)?

RAM
  • 2,413
  • 1
  • 21
  • 33
George Gao
  • 949
  • 1
  • 7
  • 5
  • Duplicate? http://stackoverflow.com/questions/8437620/analyzing-daily-weekly-data-using-ts-in-r/8438069#8438069 – Henrik Sep 12 '13 at 06:29
  • 1
    Little misconception here: for `?ts`, R gives ***"start - the time of the first observation. Either a single number or a vector of two integers"***. What you are trying to do (give a YMD date) is unknown to the ts function. –  Sep 12 '13 at 08:33

3 Answers3

5

I would suggest using the packages zoo or xts (which relies on zoo). With these time formats you can define the time series with or without daylight saving times or leap years.

Additionally I would suggest using the package lubridate to do timespan calculations. lubridate makes a difference between periods and durations.

The duration class measures the exact time span between two moments in time, that you would measure on a stop watch.

In contrast a period is for example "a month". But how long is a month? Depends on which month you mean. And for example in leap years month february has a different duration, but same period length.

Whether you need the duration or the period depends on your subject and objective. With zoo and lubridate you can choose the one that is relevant for you.

nnn
  • 4,985
  • 4
  • 24
  • 34
0

To deal with frequency for leap year, set frequency as given below:

date=c(2010,1,1)

ts(data,start=date,frequency=365+1* (!date[1]%%400 || ((date[1]%%100)&&!date[1]%%4) ))?
RAM
  • 2,413
  • 1
  • 21
  • 33
0

frequency can set to 365.25 to include leap year

ts(data,start=c(2010,1,1),frequency=365.25)
sudheera
  • 53
  • 10