You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Getting more and more enthusiastic about this package 馃槂
For lag() and lead(), also used often in data wrangling:
lag<-function (x, n=1L, default=NA, ...) {
if (length(n) !=1||!is.numeric(n) ||n<0) {
stop("n must be a nonnegative integer scalar")
}
if (n==0) {
return(x)
}
xlen<- length(x)
n<- pmin(n, xlen)
out<- c(rep(default, n), x[seq_len(xlen-n)])
attributes(out) <- attributes(x)
out
}
lead<-function (x, n=1L, default=NA, ...) {
if (length(n) !=1||!is.numeric(n) ||n<0) {
stop("n must be a nonnegative integer scalar")
}
if (n==0) {
return(x)
}
xlen<- length(x)
n<- pmin(n, xlen)
out<- c(x[-seq_len(n)], rep(default, n))
attributes(out) <- attributes(x)
out
}
Reprex:
test<-factor(letters[1:10])
lag(test)
#> [1] <NA> a b c d e f g h i #> Levels: a b c d e f g h i j
lead(test)
#> [1] b c d e f g h i j <NA>#> Levels: a b c d e f g h i j
identical(dplyr::lag(test), lag(test))
#> [1] TRUE
identical(dplyr::lead(test), lead(test))
#> [1] TRUE
The text was updated successfully, but these errors were encountered:
Getting more and more enthusiastic about this package 馃槂
For
lag()
andlead()
, also used often in data wrangling:Reprex:
The text was updated successfully, but these errors were encountered: