Re: [R] ts to irts

From: Gabor Grothendieck <ggrothendieck_at_myway.com>
Date: Sat 24 Jul 2004 - 00:57:10 EST

Jan Verbesselt <Jan.Verbesselt <at> agr.kuleuven.ac.be> writes:

: I'm working with irregular time series (time series of climate data,
: daily data 365/6 days a year) and would like to create regular time
: series from them ( irts
: e.g. Rain <-
: irts(as.POSIXct(Climate[,1]),Climate[,5])
: to ts
: e.g. test <- ts(x, start=c(1997,1), frequency=365) )
:
: such that I can find where the gaps (lacking temperature data, ...) are
: and try out methods to fill the gaps.
:
: The main objective is to detect gaps, how long they are and fill them if
: possible with average, median values.

First we create vectors of dates and values, d and val, to use as test data.

Then we create, dd, a regular time vector of dates using class Date and a corresponding logical vector not.na indicating which dates in dd correspond to observations.

Finally, we average the previous non-missing and next non-missing observation for each data point and convert the result to class ts. The last line sets the time coordinates to the numeric representation of the Date class of the corresponding dates. (If you leave it out the times will be 1,2,3,...)

d <- structure(c(11,14,20,22), class = "Date") val <- c(10,20,30,40)

dd <- seq(min(d), max(d), by = "day")
not.na <- dd %in% d

my.ts <- ts(val[cumsum(not.na)] + val[cumsum(not.na)+!not.na]/2) my.ts <- ts(my.ts, start = as.numeric(min(d)), end = as.numeric(max(d)))



R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Jul 24 01:04:23 2004

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 02:37:37 EST