[Rd] window(): feature request

Wolfgang Koller koller2@fgr.wu-wien.ac.at
Mon, 03 Apr 2000 18:21:13 +0200


Dear R-developpers,

currently the function window shows the following behaviour, if start or
end is out of range:

> x <- ts(1:14,start=c(1950,2),frequency=4)
> y <- window(x,start=c(1950,1),end=c(1953,4))
Warning messages: 
1: start value not changed in: window.default(x, ...) 
2: end value not changed in: window.default(x, ...) 

Instead of this I would like it to fill with NAs the values before start(x)
and after end(x). Could this be done by introducing an additional option in
window()? Could you please consider this in future releases? Or is there a
better way to do this using existing functions?
 
Thanks very much! 

Wolfgang Koller

P.S.: Below I have added some code that accomplishes this aim in a separate
function (which is probably an example of akward programming...).

sloppywindow <- function(x,start,end) {
  if (!is.ts(x) || (NCOL(x) > 1)) 
     stop("x is not a univariate time series") # does not work for frames
  xfrequ <- frequency(x)
  n <- length(x)

  time.start <- if (length(start) > 1) {
     if(start[2] > xfrequ) stop("invalid start")
     start[1] + (start[2] - 1)/xfrequ
  } else start

  time.end <- if (length(end) > 1) {
     if(end[2] > xfrequ) stop("invalid end")
     end[1] + (end[2] - 1)/xfrequ
  } else end

  if(time.start > time.end)
        stop("start cannot be after end")
  
  before <- round( (time(x)[1] - time.start) * xfrequ)
  before.NAs <- if (before > 0) rep(NA,before) else NULL
  after <- round( (time.end - time(x)[n] ) * xfrequ)
  after.NAs <- if (after > 0) rep(NA,after) else NULL
   
  first.start <- if (before > 0) start else start(x)
  x <- ts(c(before.NAs,x,after.NAs),start=first.start,frequency=xfrequ)  
  return(window(x,start,end))
}


-------------------------------------------------
Wolfgang Koller,  koller2@fgr.wu-wien.ac.at
Forschungsinstitut für Europafragen
Wirtschaftsuniversitaet Wien
Althanstrasse 39-45, 1090 Vienna, Austria
http://fgr.wu-wien.ac.at/institut/ef/ief-home.htm
-------------------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._