Skip to content

Commit

Permalink
monkey patch for over, #27
Browse files Browse the repository at this point in the history
  • Loading branch information
piccolbo committed Aug 12, 2015
1 parent 7e5c1fa commit e3095b2
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pkg/tests/window-functions.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@

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 <- build_sql("PARTITION BY ",sql_vector(partition,
collapse = ", ", parens = FALSE))
}
if (!is.null(order)) {
order <- build_sql("ORDER BY ", sql_vector(order, collapse = ", ", parens = FALSE))
}
if (!is.null(frame)) {
if (is.numeric(frame))
frame <- rows(frame[1], frame[2])
frame <- build_sql("ROWS ", frame)
}
over <- sql_vector(compact(list(partition, order, frame)),
parens = TRUE)
build_sql(expr, " OVER ", over)
}

environment (over) = environment(select_)

assignInNamespace(
x = "over",
ns = "dplyr",
value = over)


library(Lahman)
batting = copy_to(my_db, Batting)
batting <- select(batting, playerid, yearid, teamid, g, ab:h)
Expand All @@ -11,8 +44,10 @@ filter(players, min_rank(desc(h)) <= 2 & h > 0)
mutate(players, g_rank = min_rank(g))

# For each player, find every year that was better than the previous year
#broken
filter(players, g > lag(g))
# For each player, compute avg change in games played per year
#broken
mutate(players, g_change = (g - lag(g)) / (yearid - lag(yearid)))

# For each player, find all where they played more games than average
Expand Down

0 comments on commit e3095b2

Please sign in to comment.