I have a weekly data set imported via: tsSource=ts(sh1$I000,start=c(2004,1),freq=52) I am now getting to some 'spit and polish' but I realize something I can't wrap my head around. Given an outlier I find at say tsSource[54] ... how can get translate index 54 into the date\week. I mean I can figure out obviously that entry 52 is last week of 2004 but since the data goes for many years week 251 is a tad bit tricky since there are some years with 53 weeks in a year. Is there a function that I am missing to give me a date (or even julian week #) from a time series object? Since I am generating graphs that should read "Week starting xxx/xxx/xxx" I was hoping there was a systematic way to retrive the date from the time series object. Any suggestions? [[alternative HTML version deleted]]
Gabor Grothendieck
2010-Jan-11 22:47 UTC
[R] Getting a date out of an indice in a time series
You cannot faithfully map year and week to ts since years do not have the same number of weeks and ts can only represent regular series. If you wish to use ts for this and you want it to be faithful then use ts(x) and 1 will represent the first week, 2 the second, etc. Then if o is a Date class variable representing the origin and idx is an index then o + 7 * idx or that shifted appropriately depending on where you put the origin is the Date in question. If you are willing to go beyond ts you could look at the tis package which provides facilities for mapping to various regular series. tis does have facilities for mapping to ts. Alternately if you are willing to represent your series as an irregular series you could look at the zoo package. zoo also has facilities for mapping to ts. but using different assumptions. On Mon, Jan 11, 2010 at 2:34 PM, Idgarad <idgarad at gmail.com> wrote:> I have a weekly data set imported via: > > tsSource=ts(sh1$I000,start=c(2004,1),freq=52) > I am now getting to some 'spit and polish' but I realize something I can't > wrap my head around. > > Given an outlier I find at say tsSource[54] ... how can get translate index > 54 into the date\week. I mean I can figure out obviously that entry 52 is > last week of 2004 but since the data goes for many years week 251 is a tad > bit tricky since there are some years with 53 weeks in a year. Is there a > function that I am missing to give me a date (or even julian week #) from a > time series object? Since I am generating graphs that should read "Week > starting xxx/xxx/xxx" I was hoping there was a systematic way to retrive the > date from the time series object. Any suggestions? > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >