R-alpha: ts problems

Martyn Plummer plummer@iarc.fr
Mon, 25 Aug 1997 09:38:08 +0200 (MET DST)


This message is in MIME format
--_=XFMail.1.1.p0.Linux:970825095458:252=_
Content-Type: text/plain; charset=us-ascii

Here is a patch which fixes some problems with time series functions.
Some examples of what goes wrong...

> x <- ts(rnorm(100),start=1,deltat=2)
> start(x)
Error in ts[1] : object is not subsettable
> end(x)
Error in ts[2] : object is not subsettable
> y <- ts(matrix(rnorm(100),ncol=2))
> window(y,1,10)
Error in [.ts(x, (trunc((start - xtsp[1]) * freq + 1.5):trunc((end - xtsp[1]) *
 : unused argument to function

The first two bugs propagate quite widely. You can't even print x.

Unlike S-PLUS, R does not allow you to take row subsets of a time series
object without unclassing it (Of course there is the window function, but
you can't thin out a time series with this).   I've extended "[.ts"
to allow you to do this. I didn't put this version of "[.ts" in the patch.
Instead it is given below in full.

"[.ts" <-
function (x, i, j, drop = T)
{
        y <- NextMethod("[")
        if (missing(i))
                ts(y, start = start(x), freq = frequency(x))
        else {
                n <- if (is.matrix(x))
                        nrow(x)
                else length(x)
                ind <- (1:n)[i]
                delta <- unique(ind[-1] - ind[-length(ind)])
                if (length(delta) != 1 || delta == 0) {
                        warning("Not returning a time series object")
                }
                else {
                        xtsp <- tsp(x)
                        xtimes <- seq(from = xtsp[1], to = xtsp[2],
                                by = 1/xtsp[3])
                        ytsp <- numeric(3)
                        ytsp[1] <- xtimes[min(ind)]
                        ytsp[2] <- xtimes[max(ind)]
                        ytsp[3] <- (length(ind) - 1)/(ytsp[2] -
                                ytsp[1])
                        tsp(y) <- ytsp
                }
                y
        }
}

I hope this is useful.
Martyn

--_=XFMail.1.1.p0.Linux:970825095458:252=_
Content-Type: text/plain;
 charset=us-ascii; name=R-0.50.a3-tspatch; SizeOnDisk=613
Content-Description: R-0.50.a3-tspatch
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="R-0.50.a3-tspatch"

diff -uNr R-0.50-a1-orig/src/library/base/funs/ts R-0.50-a1/src/library/base/fun
s/ts
--- R-0.50-a1-orig/src/library/base/funs/ts	Sun Aug 24 14:51:23 1997
+++ R-0.50-a1/src/library/base/funs/ts	Sun Aug 24 14:55:26 1997
@@ -103,7 +103,7 @@
 		fs <- floor(tsp[3]*(tsp[1] - is)+0.001)
 		c(is,fs+1)
 	}
-	else ts[1]
+	else tsp[1]
 }
 
 end.ts <-
@@ -118,7 +118,7 @@
 		fs <- floor(tsp[3]*(tsp[2] - is)+0.001)
 		c(is, fs+1)
 	}
-	else ts[2]
+	else tsp[2]
 }
 
 frequency.ts <-
@@ -266,7 +266,7 @@
 }
 
 "[.ts" <-
-function(x, i, j)
+function(x, i, j, drop=T)
 {
 	y <- NextMethod("[")
 	if(is.matrix(x) & missing(i))

--_=XFMail.1.1.p0.Linux:970825095458:252=_--
End of MIME message
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-