# my own boxcar tool, just because. # use bfunc to specify what function to apply to the windowed # region. # basically must be valid function name and must accept # single value or vector of values # "pad" generates partial-width values at ends of x so output is # same length as input. # and make it optional for picky people boxcar<-function(x, width=5, bfunc='mean', pad=TRUE){ goodfunc<-try(bfunc<-get(bfunc),silent=TRUE) if (inherits (goodfunc,"try-error")) { stop ('"', bfunc,'"', ' is not a known function', call.=FALSE) } # oops two: if paranoid, force width to be integer width<-floor(width) # oops -fix width given definition of window() inputs width<-max(0,(width-1)) if (width%%2 == 1) cat('Warning: window is even length, hence asymmetric\n') #adjust start, end points to half-window width; keep output length right seqstart<- 1 -pad*((width+1)%/%2) seqend<- length(x)- width + pad*(width%/%2) boxout<-mapply(function(shiftx) { bfunc(window(x,max(shiftx,1), min(shiftx+width,length(x)) ) ) } ,seq(seqstart, seqend) ) # here's the original, simple, nopadding, etc. function #boxout<-mapply(function(shiftx) { # bfunc(window(x,shiftx,shiftx+width)) # } ,seq(1,(length(x)-width)) ) return(invisible(boxout)) }