Hi,
is there any function in R that shifts elements of a vector to the
opposite direction of what Lag() of the Hmisc package does? (something
like, Lag(x, shift = -1) )
Thanks
Zava
--------------------------------------------------------
This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}
How about
revLag <- function(x, shift=1) rev( Lag(rev(x), shift) )
x <- 1:5
revLag(x, shift=2)
As a matter of fact, here is a generalized version of Lag to include
negative shifts.
myLag <- function (x, shift = 1){
xLen <- length(x)
ret <- as.vector(character(xLen), mode = storage.mode(x))
attrib <- attributes(x)
if (!is.null(attrib$label))
atr$label <- paste(attrib$label, "lagged", shift,
"observations")
if (shift == 0) return(x)
if( xLen <= abs(shift) ) return(ret)
if (shift < 0) x <- rev(x)
retrange = 1:abs(shift)
ret[-retrange] <- x[1:(xLen - abs(shift))]
if (shift < 0) ret <- rev(ret)
attributes(ret) <- attrib
return(ret)
}
and some test examples:
myLag(1:5, shift=2)
[1] NA NA 1 2 3
myLag(letters[1:4], shift=2)
[1] "" "" "a" "b"
myLag(factor(letters[1:4]), shift=2)
[1] <NA> <NA> a b
Levels: a b c d
myLag(1:5, shift=-2)
[1] 3 4 5 NA NA
myLag(letters[1:4], shift=-2)
[1] "c" "d" "" ""
myLag(factor(letters[1:4]), shift=-2)
[1] c d <NA> <NA>
Levels: a b c d
Regards, Adai
Aydemir, Zava (FID) wrote:> Hi,
>
> is there any function in R that shifts elements of a vector to the
> opposite direction of what Lag() of the Hmisc package does? (something
> like, Lag(x, shift = -1) )
>
> Thanks
>
> Zava
> --------------------------------------------------------
>
> This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
The lag.zoo method of lag in the zoo package supports positive, negative and multiple lags and has an na.pad= argument. (zoo also has a lag.zooreg method, not shown, for zooreg objects):> library(zoo) > z <- zoo(11:15) > z1 2 3 4 5 11 12 13 14 15> lag(z, na.pad = TRUE)1 2 3 4 5 12 13 14 15 NA> lag(z, 1, na.pad = TRUE) # same1 2 3 4 5 12 13 14 15 NA> > # negative lag > lag(z, -1, na.pad = TRUE)1 2 3 4 5 NA 11 12 13 14> > # mulitple lags > lag(z, 1:3, na.pad = TRUE)lag1 lag2 lag3 1 12 13 14 2 13 14 15 3 14 15 NA 4 15 NA NA> lag(z, -(1:3), na.pad = TRUE)lag-1 lag-2 lag-3 2 11 NA NA 3 12 11 NA 4 13 12 11 5 14 13 12 vignette("zoo") # more info on zoo On 7/12/07, Aydemir, Zava (FID) <Zava.Aydemir at morganstanley.com> wrote:> Hi, > > is there any function in R that shifts elements of a vector to the > opposite direction of what Lag() of the Hmisc package does? (something > like, Lag(x, shift = -1) ) > > Thanks > > Zava > -------------------------------------------------------- > > This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >