forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
715 additions
and
27 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
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
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#' Extract the first, last or nth value from a vector. | ||
#' | ||
#' These are straightforward wrappers around \code{\link{[}}. The main | ||
#' advantage is that you can provide an optional secondary vector that defines | ||
#' the ordering. | ||
#' | ||
#' @param x A vector | ||
#' @param n For \code{nth_value}, a single integer specifying the position. | ||
#' If larger than x, an \code{NA} of the same type as x will be returned. | ||
#' @param order_by An optional vector used to determine the order | ||
#' @export | ||
#' @examples | ||
#' x <- 1:10 | ||
#' y <- 10:1 | ||
#' | ||
#' last_value(x) | ||
#' last_value(x, y) | ||
nth_value <- function(x, n, order_by = NULL) { | ||
stopifnot(length(n) == 1, is.numeric(x)) | ||
|
||
# if n > length(x), x[n] will be NA of correct type | ||
if (is.null(order_by)) x[n] else x[order(order_by)[n]] | ||
} | ||
|
||
#' @export | ||
#' @rdname nth_value | ||
first_value <- function(x, order_by = NULL) { | ||
nth_value(x, 1, order_by) | ||
} | ||
|
||
#' @export | ||
#' @rdname nth_value | ||
last_value <- function(x, order_by = NULL) { | ||
nth_value(x, length(x), order_by) | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' A helper function for ordering window function output. | ||
#' | ||
#' This is a useful function to control the order of window functions in | ||
#' R that don't have a specific ordering parameter. When translated to SQL | ||
#' it will modify the order clause of the OVER function. | ||
#' | ||
#' This function works by changing the \code{call} to instead call | ||
#' \code{\link{with_order}} with the appropriate arguments. | ||
#' | ||
#' @param order_by a vector to order_by | ||
#' @param call a function call to a window function, where the first argument | ||
#' is the vector being operated on | ||
#' @export | ||
#' @examples | ||
#' df <- data.frame(year = 2000:2005, value = (0:5) ^ 2) | ||
#' scrambled <- df[sample(nrow(df)), ] | ||
#' | ||
#' wrong <- mutate(scrambled, running = cumsum(value)) | ||
#' arrange(wrong, year) | ||
#' | ||
#' right <- mutate(scrambled, running = order_by(year, cumsum(value))) | ||
#' arrange(right, year) | ||
order_by <- function(order_by, call) { | ||
call <- substitute(call) | ||
stopifnot(is.call(call)) | ||
|
||
new_call <- as.call(c(quote(with_order), substitute(order_by), as.list(call))) | ||
eval(new_call, parent.frame()) | ||
} | ||
|
||
#' Run a function with one order, translating result back to original order | ||
#' | ||
#' This is used to power the ordering parameters of dplyr's window functions | ||
#' | ||
#' @param order_by vector to order by | ||
#' @param fun window function | ||
#' @param x,... arguments to \code{f} | ||
#' @keywords internal | ||
#' @export | ||
with_order <- function(order_by, fun, x, ...) { | ||
ord <- order(order_by) | ||
undo <- match(seq_along(order_by), ord) | ||
|
||
out <- fun(x[ord], ...) | ||
out[undo] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Purely for SQL generation: to hard to efficiently do in R. | ||
over <- function(expr, partition = NULL, order = NULL, frame = NULL) { | ||
args <- !is.null(partition) + !is.null(order) + !is.null(frame) | ||
if (args == 0) { | ||
stop("Must supply at least one of partition, order, frame", call. = FALSE) | ||
} | ||
|
||
if (!is.null(partition)) { | ||
partition <- paste0("PARTITION BY ", paste0(partition, collapse = ", ")) | ||
} | ||
if (!is.null(order)) { | ||
order <- paste0("ORDER BY ", paste0(order, collapse = ", ")) | ||
} | ||
if (!is.null(rows)) { | ||
if (is.numeric(frame)) frame <- rows(frame[1], frame[2]) | ||
rows <- paste0("ROWS ", frame) | ||
} | ||
|
||
over <- paste0(c(partition, order, rows), collapse = " ") | ||
|
||
paste0(expr, " OVER (", over, ")") | ||
} | ||
|
||
rows <- function(from = -Inf, to = 0) { | ||
if (from >= to) stop("from must be less than to", call. = FALSE) | ||
|
||
dir <- function(x) if (x < 0) "PRECEDING" else "FOLLOWING" | ||
val <- function(x) if (is.finite(x)) as.integer(abs(x)) else "UNBOUNDED" | ||
bound <- function(x) { | ||
if (x == 0) return("CURRENT ROW") | ||
paste(val(x), dir(x)) | ||
} | ||
|
||
if (to == 0) { | ||
bound(from) | ||
} else { | ||
paste0("BETWEEN ", bound(from), " AND ", bound(to)) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#' Windowed rank functions | ||
#' | ||
#' @name ranking | ||
#' @param x a vector of values to rank | ||
#' @examples | ||
#' x <- c(5, 1, 3, 2, 2) | ||
#' row_number(x) | ||
#' min_rank(x) | ||
#' dense_rank(x) | ||
#' percent_rank(x) | ||
#' cume_dist(x) | ||
#' | ||
#' ntile(x, 2) | ||
#' ntile(runif(100), 10) | ||
NULL | ||
|
||
#' @export | ||
#' @rdname ranking | ||
row_number <- function(x) order(x) | ||
|
||
# Definition from | ||
# http://blogs.msdn.com/b/craigfr/archive/2008/03/31/ranking-functions-rank-dense-rank-and-ntile.aspx | ||
#' @param n number of groups to split up into. | ||
#' @export | ||
#' @rdname ranking | ||
ntile <- function(x, n) { | ||
floor((n * (rank(x, ties.method = "first") - 1) / length(x)) + 1) | ||
} | ||
|
||
#' @export | ||
#' @rdname ranking | ||
min_rank <- function(x) base::rank(x, ties.method = "min") | ||
|
||
#' @export | ||
#' @rdname ranking | ||
dense_rank <- function(x) { | ||
r <- rank(x) | ||
match(r, sort(unique(r))) | ||
} | ||
|
||
#' @export | ||
#' @rdname ranking | ||
percent_rank <- function(x) { | ||
(min_rank(x) - 1) / (length(x) - 1) | ||
} | ||
|
||
#' @export | ||
#' @rdname ranking | ||
cume_dist <- function(x) { | ||
rank(x, ties.method = "max") / length(x) | ||
} | ||
|
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
Oops, something went wrong.