-
Notifications
You must be signed in to change notification settings - Fork 977
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
shift now accept type="shift" for more intuitive give.names=T output,… (
- Loading branch information
1 parent
fbd5b60
commit 6e60d7b
Showing
5 changed files
with
36 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
shift <- function(x, n=1L, fill=NA, type=c("lag", "lead"), give.names=FALSE) { | ||
shift <- function(x, n=1L, fill=NA, type=c("lag", "lead", "shift"), give.names=FALSE) { | ||
type = match.arg(type) | ||
ans = .Call(Cshift, x, as.integer(n), fill, type) | ||
if (give.names) { | ||
if (give.names && is.list(ans)) { | ||
if (is.null(names(x))) { | ||
xsub = substitute(x) | ||
if (is.atomic(x) && is.name(xsub)) nx = deparse(xsub, 500L) | ||
else nx = paste0("V", if (is.atomic(x)) 1L else seq_along(x)) | ||
} | ||
else nx = names(x) | ||
setattr(ans, 'names', do.call("paste", c(CJ(nx, type, n, sorted=FALSE), sep="_"))) | ||
if (type!="shift") { | ||
# flip type for negative n, #3223 | ||
neg = (n<0L) | ||
if (type=="lead") neg[ n==0L ] = TRUE # lead_0 should be named lag_0 for consistency | ||
if (any(neg)) { | ||
type = rep(type,length(n)) | ||
type[neg] = if (type[1L]=="lead") "lag" else "lead" | ||
n[neg] = -n[neg] | ||
} | ||
} | ||
setattr(ans, "names", paste(rep(nx,each=length(n)), type, n, sep="_")) | ||
} | ||
ans | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters