Skip to content

Commit

Permalink
Improvements to lag2_ and possible bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicChr committed May 1, 2024
1 parent 04e0cd0 commit 90de63a
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 133 deletions.
27 changes: 13 additions & 14 deletions R/lag.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
#' @details
#' For most applications, it is more efficient and recommended to use `lag_()`.
#' For anything that requires dynamic lags, lag by order of another variable,
#' or by-group lags, one can use `lag2_()`.
#' or by-group lags, one can use `lag2_()`. \cr
#' To do cyclic lags, see the examples below for an implementation.
#'
#' ### `lag2_`
#'
Expand Down Expand Up @@ -99,8 +100,8 @@
#' # lag every 2nd element
#' lag2_(x, n = c(1, 0)) # lag vector is recycled
#'
#' # Explicit lags along x
#' lags <- lag_sequence(length(x), 1, partial = FALSE)
#' # Explicit Lag(3) using a vector of lags
#' lags <- lag_sequence(length(x), 3, partial = FALSE)
#' lag2_(x, n = lags)
#'
#' # Alternating lags and leads
Expand All @@ -124,12 +125,13 @@
#'
#' # Example of how to do a cyclical lag
#' n <- length(x)
#'
#' # When k >= 0
#' k <- min(3, n)
#' if (k >= 0){
#' lag2_(x, c(rep(-n + k, k), rep(k, n - k)))
#' } else {
#' lag2_(x, c(rep(k, n + k), rep(n + k, -k)))
#' }
#' lag2_(x, c(rep(-n + k, k), rep(k, n - k)))
#' # When k < 0
#' k <- max(-3, -n)
#' lag2_(x, c(rep(k, n + k), rep(n + k, -k)))
#'
#' # As it turns out, we can do a grouped lag
#' # by supplying group sizes as run lengths and group order as the order
Expand Down Expand Up @@ -185,13 +187,15 @@
#' # Let's compare this to data.table
#'
#' library(data.table)
#' setDTthreads(2)
#' default_threads <- getDTthreads()
#' setDTthreads(1)
#' dt <- data.table(x = 1:10^5,
#' g = sample.int(10^4, 10^5, TRUE))
#'
#' bench::mark(dt[, y := shift(x), by = g][][["y"]],
#' grouped_lag(dt$x, g = dt$g),
#' iterations = 10)
#' setDTthreads(default_threads)
#' @rdname lag
#' @export
lag_ <- function(x, n = 1L, fill = NULL, set = FALSE, recursive = TRUE){
Expand All @@ -202,8 +206,3 @@ lag_ <- function(x, n = 1L, fill = NULL, set = FALSE, recursive = TRUE){
lag2_ <- function(x, n = 1L, order = NULL, run_lengths = NULL, fill = NULL, recursive = TRUE){
.Call(`_cheapr_cpp_lag2`, x, n, order, run_lengths, fill, recursive)
}
# lag2_ <- function(x, n = 1L, order = if (recursive && is.data.frame(x)) seq_len(nrow(x)) else seq_along(x),
# run_lengths = if (recursive && is.data.frame(x)) nrow(x) else length(x),
# fill = NULL, recursive = TRUE){
# .Call(`_cheapr_cpp_lag2`, x, n, order, run_lengths, fill, recursive)
# }
22 changes: 13 additions & 9 deletions man/lag.Rd

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

4 changes: 2 additions & 2 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ extern "C" SEXP _cheapr_cpp_lcm2_vectorised(SEXP x, SEXP y, SEXP tol, SEXP na_rm
END_CPP11
}
// lag.cpp
SEXP cpp_lag(SEXP x, int k, SEXP fill, bool set, bool recursive);
SEXP cpp_lag(SEXP x, R_xlen_t k, SEXP fill, bool set, bool recursive);
extern "C" SEXP _cheapr_cpp_lag(SEXP x, SEXP k, SEXP fill, SEXP set, SEXP recursive) {
BEGIN_CPP11
return cpp11::as_sexp(cpp_lag(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x), cpp11::as_cpp<cpp11::decay_t<int>>(k), cpp11::as_cpp<cpp11::decay_t<SEXP>>(fill), cpp11::as_cpp<cpp11::decay_t<bool>>(set), cpp11::as_cpp<cpp11::decay_t<bool>>(recursive)));
return cpp11::as_sexp(cpp_lag(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x), cpp11::as_cpp<cpp11::decay_t<R_xlen_t>>(k), cpp11::as_cpp<cpp11::decay_t<SEXP>>(fill), cpp11::as_cpp<cpp11::decay_t<bool>>(set), cpp11::as_cpp<cpp11::decay_t<bool>>(recursive)));
END_CPP11
}
// lag.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/gcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ SEXP cpp_lcm(SEXP x, double tol, bool na_rm){
lcm = NA_REAL;
}
int lcm_int = p_x[0];
double int_max = double(integer_max_);
double int_max = integer_max_;
for (int i = 1; i < n; ++i) {
if (!na_rm && !(lcm == lcm)){
lcm = NA_REAL;
Expand Down
Loading

0 comments on commit 90de63a

Please sign in to comment.