Skip to content

Commit

Permalink
Added function.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicChr committed Oct 9, 2024
1 parent 6e4a8cc commit 5c68480
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 40 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export(any_na)
export(as_discrete)
export(as_factor)
export(bin)
export(cheapr_var)
export(col_all_na)
export(col_any_na)
export(col_na_counts)
Expand Down
40 changes: 32 additions & 8 deletions R/extras.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#' randomly into your vector.
#' @param prop Proportion of scalar values (or `NA`) values to insert
#' randomly into your vector.
#' @param na.rm Should `NA` values be ignored in `cheapr_var()` Default is
#' `TRUE`.
#'
#' @returns
#' `enframe()_` converts a vector to a data frame. \cr
Expand All @@ -40,14 +42,8 @@
#' Useful for generating missing data. \cr
#' `vector_length` behaves mostly like `NROW()` except
#' for matrices in which it matches `length()`.
#'
#' @details
#' `intersect_()` and `setdiff_()` are faster and more efficient
#' alternatives to `intersect()` and `setdiff()` respectively. \cr
#' `enframe_()` and `deframe_()` are faster alternatives to
#' `tibble::enframe()` and `tibble::deframe()` respectively. \cr
#' `cut_numeric()` is a faster and more efficient alternative to
#' `cut.default()`.
#' `cheapr_var` returns the variance of a numeric vector.
#' No coercion happens for integer vectors and so is very cheap.
#'
#' @export
#' @rdname extras
Expand Down Expand Up @@ -206,6 +202,34 @@ na_insert <- function(x, n = NULL, prop = NULL){
#' @export
#' @rdname extras
vector_length <- cpp_vec_length
#' @export
#' @rdname extras
cheapr_var <- function(x, na.rm = TRUE){
if (is.integer(x)){
y <- as.integer(x)
} else {
y <- as.double(x)
}
if (na.rm){
N <- (length(y) - na_count(y)) - 1
} else {
N <- length(y) - 1
}
if (N < 1){
return(NA_real_)
} else {
mu <- collapse::fmean(y, na.rm = na.rm)
if (is.na(mu)){
NA_real_
} else {
var_sum_squared_diff(y, as.double(mu)) / N
}
}
}
cheapr_sd <- function(x, na.rm = TRUE){
sqrt(cheapr_var(x, na.rm = na.rm))
}


# head_ <- function(x, n = 1L){
# check_length(n, 1L)
Expand Down
23 changes: 0 additions & 23 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,26 +148,3 @@ n_dots <- function(...){

# Keep this in-case anyone was using it
fill_with_na <- na_insert

# Basically if x is integer no vector conversion to double happens
cheapr_var <- function(x, na.rm = TRUE){
if (is.integer(x)){
y <- as.integer(x)
} else {
y <- as.double(x)
}
if (na.rm){
N <- (length(y) - na_count(y)) - 1
} else {
N <- length(y) - 1
}
mu <- collapse::fmean(y, na.rm = na.rm)
if (length(mu) < 1 || any_na(mu)){
NA_real_
} else {
var_sum_squared_diff(y, as.double(mu)) / N
}
}
cheapr_sd <- function(x, na.rm = TRUE){
sqrt(cheapr_var(x, na.rm = na.rm))
}
2 changes: 1 addition & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
* Updated to version 0.9.8
* Updated to version 0.9.9

* Checked and passed using rhub v2.0.0 in the following environments:

Expand Down
16 changes: 8 additions & 8 deletions man/extras.Rd

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

0 comments on commit 5c68480

Please sign in to comment.