Skip to content

window(): feature request

2 messages · Wolfgang Koller, Brian Ripley

#
Dear R-developpers,

currently the function window shows the following behaviour, if start or
end is out of range:
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
4 days later
#
On Mon, 3 Apr 2000, Wolfgang Koller wrote:

            
This is incompatible with S and the existing behaviour, so I have added a
new argument `extend' defaulting to FALSE. The new version is in the
R-devel distribution, so will not appear in a released version until 1.1.0.
[It does not handle matrix ts objects, for example, nor does it handle
changing the frequency.]

[code snipped.]

PS: R-bugs now has a `wishlist' section, so sending a feature request as a
bug report is a way to ensure it gets recorded.