Skip to content

Commit

Permalink
New function 'lag_'.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicChr committed Apr 17, 2024
1 parent 3cfea64 commit f71325a
Show file tree
Hide file tree
Showing 11 changed files with 826 additions and 100 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export(gcd)
export(gcd2)
export(intersect_)
export(is_na)
export(lag_)
export(lag_sequence)
export(lead_sequence)
export(lengths_)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# cheapr (Development version)

* New function `lag_` for very fast lags and leads on vectors and data frames.
It includes a `set` argument allowing users to create a lagged vector
by reference without copies.

* `set_round` has been amended to improve floating point accuracy.

# cheapr 0.8.0

* New 'set' Math operations inspired by 'data.table' and 'collapse'
Expand Down
8 changes: 8 additions & 0 deletions R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ cpp_lcm2_vectorised <- function(x, y, tol, na_rm) {
.Call(`_cheapr_cpp_lcm2_vectorised`, x, y, tol, na_rm)
}

cpp_lag <- function(x, k, fill, set, recursive) {
.Call(`_cheapr_cpp_lag`, x, k, fill, set, recursive)
}

cpp_num_na <- function(x, recursive) {
.Call(`_cheapr_cpp_num_na`, x, recursive)
}
Expand Down Expand Up @@ -224,6 +228,10 @@ cpp_list_as_df <- function(x) {
.Call(`_cheapr_cpp_list_as_df`, x)
}

r_copy <- function(x) {
.Call(`_cheapr_r_copy`, x)
}

cpp_which_ <- function(x, invert) {
.Call(`_cheapr_cpp_which_`, x, invert)
}
Expand Down
40 changes: 40 additions & 0 deletions R/lag.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#' Lagged operations.
#'
#' @description
#' Fast lags and leads
#'
#' @param x A vector.
#' @param n Number of lags. Negative values are accepted.
#' @param fill Value used to fill first n values. Default is `NA`.
#' @param set Should x be updated by reference? If `TRUE` no copy is made and
#' x is updated in place. The default is `FALSE`.
#' @param recursive Should list elements be lagged as well?
#' If `TRUE`, this is useful for data frames and will return row lags.
#' If `FALSE` this will return a plain lagged list.
#'
#' @returns
#' A lagged object the same size as x.
#'
#' @examples
#' library(cheapr)
#' library(bench)
#'
#' # A use-case for data.table
#'
#' df <- data.frame(x = 1:10^5)
#'
#' # Lag these behind by 3 rows
#' lag_(df, 3, set = TRUE)
#'
#' sset(df, 1:5) # x variable was updated by reference!
#'
#' # The above can be used naturally in data.table to lag data
#' # without any copies
#'
#' # To perform regular R row lags, just make sure set is `FALSE`
#'
#' sset(lag_(as.data.frame(EuStockMarkets), 5), 1:10)
#' @export
lag_ <- function(x, n = 1, fill = NULL, set = FALSE, recursive = TRUE){
.Call(`_cheapr_cpp_lag`, x, n, fill, set, recursive)
}
14 changes: 0 additions & 14 deletions R/sset.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,6 @@ sset.default <- function(x, i, ...){
#' @rdname sset
#' @export
sset.Date <- function(x, i, ...){
# old_class <- oldClass(x)
# set_rm_attr(x, "class")
# on.exit({invisible(set_attr(x, "class", old_class))})
# out <- sset.default(x, i, ...)
# set_attr(out, "class", old_class)
# out <- sset.default(unclass(x), i, ...)
# cls <- oldClass(x)
# class(x) <- NULL
# out <- NextMethod("sset")
# set_attr(out, "class", cls)


# out <- sset.default(unclass(x), i, ...)
# set_attr(out, "class", oldClass(x))
if (!missing(i) && is.logical(i)){
check_length(i, length(x))
i <- which_(i)
Expand Down
48 changes: 48 additions & 0 deletions man/lag_.Rd

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

16 changes: 16 additions & 0 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ extern "C" SEXP _cheapr_cpp_lcm2_vectorised(SEXP x, SEXP y, SEXP tol, SEXP na_rm
return cpp11::as_sexp(cpp_lcm2_vectorised(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x), cpp11::as_cpp<cpp11::decay_t<SEXP>>(y), cpp11::as_cpp<cpp11::decay_t<double>>(tol), cpp11::as_cpp<cpp11::decay_t<bool>>(na_rm)));
END_CPP11
}
// lag.cpp
SEXP cpp_lag(SEXP x, int 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)));
END_CPP11
}
// nas.cpp
SEXP cpp_num_na(SEXP x, bool recursive);
extern "C" SEXP _cheapr_cpp_num_na(SEXP x, SEXP recursive) {
Expand Down Expand Up @@ -397,6 +404,13 @@ extern "C" SEXP _cheapr_cpp_list_as_df(SEXP x) {
return cpp11::as_sexp(cpp_list_as_df(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x)));
END_CPP11
}
// utils.cpp
SEXP r_copy(SEXP x);
extern "C" SEXP _cheapr_r_copy(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(r_copy(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x)));
END_CPP11
}
// which.cpp
SEXP cpp_which_(SEXP x, bool invert);
extern "C" SEXP _cheapr_cpp_which_(SEXP x, SEXP invert) {
Expand Down Expand Up @@ -426,6 +440,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_cheapr_cpp_gcd2_vectorised", (DL_FUNC) &_cheapr_cpp_gcd2_vectorised, 4},
{"_cheapr_cpp_int_sequence", (DL_FUNC) &_cheapr_cpp_int_sequence, 3},
{"_cheapr_cpp_is_na", (DL_FUNC) &_cheapr_cpp_is_na, 1},
{"_cheapr_cpp_lag", (DL_FUNC) &_cheapr_cpp_lag, 5},
{"_cheapr_cpp_lag_sequence", (DL_FUNC) &_cheapr_cpp_lag_sequence, 3},
{"_cheapr_cpp_lcm", (DL_FUNC) &_cheapr_cpp_lcm, 3},
{"_cheapr_cpp_lcm2", (DL_FUNC) &_cheapr_cpp_lcm2, 4},
Expand Down Expand Up @@ -472,6 +487,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_cheapr_cpp_which_val", (DL_FUNC) &_cheapr_cpp_which_val, 3},
{"_cheapr_cpp_window_sequence", (DL_FUNC) &_cheapr_cpp_window_sequence, 4},
{"_cheapr_is_alt_compact_seq", (DL_FUNC) &_cheapr_is_alt_compact_seq, 1},
{"_cheapr_r_copy", (DL_FUNC) &_cheapr_r_copy, 1},
{NULL, NULL, 0}
};
}
Expand Down
Loading

0 comments on commit f71325a

Please sign in to comment.