diff --git a/NAMESPACE b/NAMESPACE index b438428..5c40bf3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,8 +1,14 @@ # Generated by roxygen2: do not edit by hand S3method(base::as.character,vctrs_rcrd) +S3method(collapse::fmean,integer64) +S3method(collapse::fmedian,integer64) +S3method(collapse::fnobs,integer64) +S3method(collapse::fnth,integer64) +S3method(collapse::fsd,integer64) S3method(collapse::funique,POSIXlt) S3method(collapse::funique,vctrs_rcrd) +S3method(collapse::fvar,integer64) S3method(cut,integer64) S3method(is_na,POSIXlt) S3method(is_na,data.frame) diff --git a/R/factors.R b/R/factors.R index c958880..8934f63 100644 --- a/R/factors.R +++ b/R/factors.R @@ -75,8 +75,6 @@ factor_ <- function( if (inherits(lvls, "data.frame")){ fct_lvls <- do.call(paste, c(lvls, list(sep = "_"))) } else if (is_int64){ - # fct_lvls <- formatC(lvls, format = "f", drop0trailing = TRUE) - # fct_lvls <- format(lvls, scientific = FALSE, trim = TRUE) fct_lvls <- cpp_format_numeric_as_int64(lvls) } else { fct_lvls <- as.character(lvls) diff --git a/R/utils.R b/R/utils.R index 341019e..265969d 100644 --- a/R/utils.R +++ b/R/utils.R @@ -141,6 +141,31 @@ funique.POSIXlt <- function(x, sort = FALSE, ...){ out } +#' @exportS3Method collapse::fmean +fmean.integer64 <- function(x, ...){ + collapse::fmean(cpp_int64_to_double(x), ...) +} +#' @exportS3Method collapse::fmedian +fmedian.integer64 <- function(x, ...){ + collapse::fmedian(cpp_int64_to_double(x), ...) +} +#' @exportS3Method collapse::fvar +fvar.integer64 <- function(x, ...){ + collapse::fvar(cpp_int64_to_double(x), ...) +} +#' @exportS3Method collapse::fsd +fsd.integer64 <- function(x, ...){ + collapse::fsd(cpp_int64_to_double(x), ...) +} +#' @exportS3Method collapse::fnth +fnth.integer64 <- function(x, ...){ + collapse::fnth(cpp_int64_to_double(x), ...) +} +#' @exportS3Method collapse::fnobs +fnobs.integer64 <- function(x, ...){ + collapse::fnobs(cpp_int64_to_double(x), ...) +} + n_dots <- function(...){ nargs() } diff --git a/src/attrs.cpp b/src/attrs.cpp index aa5b2e3..c5084db 100644 --- a/src/attrs.cpp +++ b/src/attrs.cpp @@ -41,7 +41,9 @@ SEXP cpp_set_rm_attr(SEXP x, SEXP which) { [[cpp11::register]] SEXP cpp_set_add_attributes(SEXP x, SEXP attributes, bool add) { int NP = 0; - if (Rf_isNull(attributes)){ + if (Rf_isNull(attributes) || + // is.null or empty list? + (TYPEOF(attributes) == VECSXP && Rf_length(attributes) == 0)){ if (add){ return x; } else { diff --git a/src/scalars.cpp b/src/scalars.cpp index 3ececbf..7d50d17 100644 --- a/src/scalars.cpp +++ b/src/scalars.cpp @@ -181,6 +181,36 @@ SEXP cpp_val_replace(SEXP x, SEXP value, SEXP replace, bool recursive){ break; } case REALSXP: { + if (is_int64(x)){ + SEXP temp = Rf_protect(Rf_allocVector(REALSXP, 0)); ++NP; + long long *p_out = INTEGER64_PTR(temp); + + if (!is_int64(value)){ + Rf_protect(value = Rf_coerceVector(value, REALSXP)); ++NP; + Rf_protect(value = cpp_numeric_to_int64(value)); ++NP; + } + if (!is_int64(replace)){ + Rf_protect(replace = Rf_coerceVector(replace, REALSXP)); ++NP; + Rf_protect(replace = cpp_numeric_to_int64(replace)); ++NP; + } + long long val = INTEGER64_PTR(value)[0]; + long long repl = INTEGER64_PTR(replace)[0]; + long long *p_x = INTEGER64_PTR(x); + for (R_xlen_t i = 0; i < n; ++i){ + eq = p_x[i] == val; + if (!any_eq && eq){ + any_eq = true; + out = Rf_protect(Rf_duplicate(x)); ++NP; + // Change where pointer is pointing to + p_out = INTEGER64_PTR(out); + } + if (eq) p_out[i] = repl; + } + // Make sure to return x if there were no values to replace + if (!any_eq){ + out = Rf_protect(x); ++NP; + } + } else { SEXP temp = Rf_protect(Rf_allocVector(REALSXP, 0)); ++NP; double *p_out = REAL(temp); Rf_protect(value = Rf_coerceVector(value, REALSXP)); ++NP; @@ -219,7 +249,8 @@ SEXP cpp_val_replace(SEXP x, SEXP value, SEXP replace, bool recursive){ out = Rf_protect(x); ++NP; } } - break; + } + break; } case STRSXP: { Rf_protect(value = Rf_coerceVector(value, STRSXP)); ++NP;