[R] Construct time series objects from raw data stored in csv files

Gabor Grothendieck ggrothendieck at gmail.com
Fri Apr 13 02:26:54 CEST 2007


On 4/12/07, tom soyer <tom.soyer at gmail.com> wrote:
> What should I do for weekly and daily data series? Are there functions
> similar to yearmon() for other time intervals? I see that there is a
> built-in yearqtr() function for quarterly data, but that's it.

ts series cannot directly represent daily and weekly
series other than somehow deciding on a numeric representation
for time.

Here we will use the number of days and the number of weeks
since the Epoch (1970-01-01) and assume we assume weeks
start on Sunday.  We will also do it over again using the number
of weeks since the first point in the series and the number of
days since the first point.

Lines.raw is from my original post on this thread.


z <- read.zoo(textConnection(Lines.raw), header = TRUE, sep = ",")

# ts series will represent days as no of days since Epoch
zday <- z
frequency(zday) <- 1
tsday <- as.ts(zday)

# ts series will represent weeks as no of weeks since Epoch
zweek <- zday
offset <- -3 # weeks start on Sun
# offset <- -4 # weeks start on Mon
zweek <- aggregate(z, (as.numeric(time(z)) + offset) %/% 7, mean)
frequency(zweek) <- 1
tsweek <- as.ts(zweek)

##########################################################
# alternately use number of days since first day in series
# and number of weeks since first week in series


zday0 <- aggregate(zday, time(zday) - min(time(zday)), c)
frequency(zday0) <- 1
tsday0 <- as.ts(zday0)

zweek0 <- aggregate(zweek, time(zweek) - min(time(zweek)), c)
frequency(zweek0) <- 1
tsweek0 <- as.ts(zweek0)



More information about the R-help mailing list