| Title: | Enhanced 'mutate' with 'Apache Spark' Style Window Operations |
|---|---|
| Description: | Window operations for R dataframes with 'by', 'order_by' and 'frame' defined by 'rows_between' or 'range_between', inspired by 'Apache Spark' via 'mutate' in 'dplyr' flavor. |
| Authors: | Srikanth Komala Sheshachala [aut, cre] |
| Maintainer: | Srikanth Komala Sheshachala <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.3.0 |
| Built: | 2026-10-02 23:44:53 UTC |
| Source: | https://github.com/talegari/tidier |
Provides supercharged version of dplyr::mutate
with .by (group by), .order_by and .frame aggregation over arbitrary
window frame
mutate(x, ..., .by, .order_by, .frame, .complete = FALSE)mutate(x, ..., .by, .order_by, .frame, .complete = FALSE)
x |
( |
... |
expressions to be passed to dplyr::mutate |
.by |
(expression, optional: Yes) Columns to group by |
.order_by |
(expression, optional: Yes) Columns to order by |
.frame |
(vector, optional: Yes) Object of class |
.complete |
(flag, default: FALSE) passed to slider::slide |
A window function returns a value for every input row of a dataframe
based on a group of rows (frame) in the neighborhood of the input row. This
function implements computation over groups (partition_by in SQL) in a
predefined order (order_by in SQL) across a neighborhood of rows (frame)
defined by
rows_between: Number of rows before and after the corresponding row.
Example: c(2, 1)
range_between: Range is numeric or interval objects
Example: c(days(2), days(1))
This implementation is inspired by spark's window API.
The output has the same row order as the input independent of the
order_by.
data.frame
rows_between(), range_between(), mutate()
library("magrittr") # for pipe # example 1: rows between # Using iris dataset, # compute cumulative mean of column `Sepal.Length` # ordered by `Petal.Width` and `Sepal.Width` columns # grouped by `Petal.Length` column iris %>% mutate(sl_mean = mean(Sepal.Length), .order_by = c(Petal.Width, Sepal.Width), .by = Petal.Length, .frame = rows_between(Inf, 0), ) %>% dplyr::slice_min(n = 3, Petal.Width, by = Species) # example 2: range between # Using a sample airquality dataset, # compute mean temp over last seven days in the same month for every row set.seed(101) airquality %>% # create date column dplyr::mutate(date_col = lubridate::make_date(1973, Month, Day)) %>% # create gaps by removing some days dplyr::slice_sample(prop = 0.8) %>% # compute mean temperature over last seven days in the same month tidier::mutate(avg_temp_over_last_week = mean(Temp, na.rm = TRUE), .order_by = date_col, .by = Month, .frame = range_between( lubridate::days(7), # 7 days before current row lubridate::days(-1) # do not include current row ) ) # example 3: custom function / modeling over window frame fit_lm_safe = function(df, formula, min_rows = 2) { if (is.null(df) || nrow(df) < min_rows) { return(NULL) } tryCatch( lm(formula, data = df), error = function(e) NULL ) } mtcars %>% mutate(s = list(fit_lm_safe(pick(everything()), mpg ~ .)), .by = c(cyl, vs), .order_by = qsec, .frame = range_between(-1, 3), .complete = FALSE ) %>% head(10) ## Not run: # example 4: parallel execution across many groups using tidyr::nest and furrr future::plan(future::multisession(workers = 2)) iris %>% tidyr::nest(.by = Species) %>% dplyr::mutate( data = furrr::future_map(data, ~ .x %>% mutate( sl_mean = mean(Sepal.Length), .order_by = Petal.Width, .frame = rows_between(2, 2) ) ) ) %>% tidyr::unnest(data) ## End(Not run)library("magrittr") # for pipe # example 1: rows between # Using iris dataset, # compute cumulative mean of column `Sepal.Length` # ordered by `Petal.Width` and `Sepal.Width` columns # grouped by `Petal.Length` column iris %>% mutate(sl_mean = mean(Sepal.Length), .order_by = c(Petal.Width, Sepal.Width), .by = Petal.Length, .frame = rows_between(Inf, 0), ) %>% dplyr::slice_min(n = 3, Petal.Width, by = Species) # example 2: range between # Using a sample airquality dataset, # compute mean temp over last seven days in the same month for every row set.seed(101) airquality %>% # create date column dplyr::mutate(date_col = lubridate::make_date(1973, Month, Day)) %>% # create gaps by removing some days dplyr::slice_sample(prop = 0.8) %>% # compute mean temperature over last seven days in the same month tidier::mutate(avg_temp_over_last_week = mean(Temp, na.rm = TRUE), .order_by = date_col, .by = Month, .frame = range_between( lubridate::days(7), # 7 days before current row lubridate::days(-1) # do not include current row ) ) # example 3: custom function / modeling over window frame fit_lm_safe = function(df, formula, min_rows = 2) { if (is.null(df) || nrow(df) < min_rows) { return(NULL) } tryCatch( lm(formula, data = df), error = function(e) NULL ) } mtcars %>% mutate(s = list(fit_lm_safe(pick(everything()), mpg ~ .)), .by = c(cyl, vs), .order_by = qsec, .frame = range_between(-1, 3), .complete = FALSE ) %>% head(10) ## Not run: # example 4: parallel execution across many groups using tidyr::nest and furrr future::plan(future::multisession(workers = 2)) iris %>% tidyr::nest(.by = Species) %>% dplyr::mutate( data = furrr::future_map(data, ~ .x %>% mutate( sl_mean = mean(Sepal.Length), .order_by = Petal.Width, .frame = rows_between(2, 2) ) ) ) %>% tidyr::unnest(data) ## End(Not run)
Create a frame indicating range between window
range_between(before, after)range_between(before, after)
before |
range of rows before |
after |
range of rows after |
object of class range_between_frame, frame
rows_between(), range_between(), mutate()
Create a frame indicating rows between window
rows_between(before, after)rows_between(before, after)
before |
Number of rows before |
after |
Number of rows after |
object of class rows_between_frame, frame
rows_between(), range_between(), mutate()