Skip to content

Commit

Permalink
fix(vec_position_lag): length->vec_size + better Date perf + fix docs
Browse files Browse the repository at this point in the history
Fix length -> vec_size.

Combining (logical/generic) NA with Dates is apparently slow.  Slice with
NA_integer_ index instead.

Fix docs: dplyr 1.1.0 should also have a generalized dplyr::lag. Removing some
dplyr::lag features for speed might be another motivation, but we seem to be
faster for some ptypes x sizes and slower for others.

Also, don't export this function; we don't need to.
  • Loading branch information
brookslogan committed Jan 14, 2025
1 parent c99125d commit 0208a96
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export(time_column_names)
export(ungroup)
export(unnest)
export(validate_epi_archive)
export(vec_position_lag)
export(version_column_names)
import(epidatasets)
importFrom(checkmate,anyInfinite)
Expand Down
19 changes: 6 additions & 13 deletions R/archive.R
Original file line number Diff line number Diff line change
Expand Up @@ -371,35 +371,28 @@ removed_by_compactify <- function(df, keys, tolerance) {

#' Lag entries in a vctrs-style vector by their position in the vector
#'
#' This is meant to be more general than [`dplyr::lag()`] in terms of the kinds
#' of `x` that it accepts, allowing any `{vctrs}`-sense vector.
#' This is like [`dplyr::lag`], though it has different performance
#' characteristics. It's unclear whether we want to use this.
#'
#' @examples
#'
#' vec_position_lag(1:5, 3)
#' dplyr::lag(1:5, 3)
#'
#'
#' # Try on a more exotic class:
#' vec_position_lag(epipredict::dist_quantiles(list(1:6, 11:16), 0:5 / 5), 1)
#' \dontrun{
#' # XXX We were having trouble with something like this. dplyr 1.1.0 seems like it
#' # fixes this, but our troubles were too recent for dplyr 1.1.0 to have fixed,
#' # I would have thought. Need to check for what the failing example actually was.
#' dplyr::lag(epipredict::dist_quantiles(list(1:6, 11:16), 0:5 / 5), 1)
#' }
#'
#' @importFrom checkmate assert_count
#' @importFrom vctrs obj_check_vector vec_slice vec_size
#' @importFrom vctrs obj_check_vector vec_c vec_slice vec_size
#' @keywords internal
#' @importFrom vctrs vec_c vec_slice vec_size
#' @export
vec_position_lag <- function(x, n) {
obj_check_vector(x)
assert_count(n)
if (length(x) <= n) {
vec_c(rep(NA, length(x)), vec_slice(x, integer()))
vec_slice(x, rep(NA_integer_, vec_size(x)))
} else {
vec_c(rep(NA, n), vec_slice(x, seq_len(vec_size(x) - n)))
vec_slice(x, c(rep(NA_integer_, n), seq_len(vec_size(x) - n)))
}
}

Expand Down
8 changes: 3 additions & 5 deletions man/vec_position_lag.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0208a96

Please sign in to comment.