From 962f8ea6b7efacbabc8497ae4e1f89390c218945 Mon Sep 17 00:00:00 2001 From: Jared Knowles Date: Thu, 19 Sep 2024 14:32:43 -0400 Subject: [PATCH] add rounding utilities --- DESCRIPTION | 52 +++++++++++++-------------- Jenkinsfile | 6 ++-- NAMESPACE | 3 ++ R/utils.R | 69 ++++++++++++++++++++++++++++++++++++ man/rnh.Rd | 20 +++++++++++ man/round_to_nearest_half.Rd | 22 ++++++++++++ man/trim_max.Rd | 24 +++++++++++++ 7 files changed, 167 insertions(+), 29 deletions(-) create mode 100644 man/rnh.Rd create mode 100644 man/round_to_nearest_half.Rd create mode 100644 man/trim_max.Rd diff --git a/DESCRIPTION b/DESCRIPTION index edd9d9e..c4f2f03 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,26 +1,26 @@ -Package: civilytics -Type: Package -Title: Utilities Functions for Civilytics -Version: 0.1.0 -Author: Jared E. Knowles -Maintainer: Jared E. Knowles -Description: House R functions for Civilytics Consulting LLC - This package implements a variety of useful functions for creating and - branding analyses produced by Civilytics Consulting LLC. -License: LGPL (>= 3) -Depends: - R (>= 2.15.1) -Imports: - ggplot2, - jpeg, - png, - stringr, - gridExtra, - tidycensus, - grid, - stringdist -Encoding: UTF-8 -LazyData: true -Suggests: - testthat -RoxygenNote: 7.3.2 +Package: civilytics +Type: Package +Title: Utilities Functions for Civilytics +Version: 0.2.0 +Author: Jared E. Knowles +Maintainer: Jared E. Knowles +Description: House R functions for Civilytics Consulting LLC + This package implements a variety of useful functions for creating and + branding analyses produced by Civilytics Consulting LLC. +License: LGPL (>= 3) +Depends: + R (>= 2.15.1) +Imports: + ggplot2, + jpeg, + png, + stringr, + gridExtra, + tidycensus, + grid, + stringdist +Encoding: UTF-8 +LazyData: true +Suggests: + testthat +RoxygenNote: 7.3.2 diff --git a/Jenkinsfile b/Jenkinsfile index 675b5f2..c291bcf 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -24,11 +24,11 @@ pipeline { stage('Check') { steps { sh ''' - R CMD check --no-manual civilytics_0.1.0.tar.gz + R CMD check --no-manual civilytics_0.2.0.tar.gz ''' sh ''' - R CMD INSTALL civilytics_0.1.0.tar.gz + R CMD INSTALL civilytics_0.2.0.tar.gz ''' } } @@ -50,7 +50,7 @@ pipeline { steps { sh ''' - rm -rf civilytics_0.1.0.tar.gz civilytics.Rcheck + rm -rf civilytics_0.2.0.tar.gz civilytics.Rcheck ''' } } diff --git a/NAMESPACE b/NAMESPACE index 04ba561..5816444 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -27,11 +27,14 @@ export(pretty_count) export(pretty_per) export(race_short_names) export(random_round) +export(rnh) +export(round_to_nearest_half) export(safe_max) export(safe_ratio) export(simpleCap) export(star_subs) export(theme_civilytics) +export(trim_max) export(waldInterval) export(z_gap_test) export(z_univariate) diff --git a/R/utils.R b/R/utils.R index 9853a4b..9c3c99c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -376,3 +376,72 @@ safe_ratio <- function(num, denom) { y <- num / denom return(y) } + + + +#' Take the maximum of a number after trimming values +#' +#' @param vec a numeric vector +#' @param n integer, the number of maximum values to trim before taking the maximum +#' +#' @return the highest value after removing the highest n values +#' @export +#' +#' @examples +#' trim_max(c(10, 10, 10, 9, 8, 7), n = 2) +#' trim_max(c(10, 10, 10, 9, 8, 7), n = 3) +#' trim_max(c(10, 10, 10, 9, 8, 7), n = 4) +trim_max <- function(vec, n) { + # Sort vector ascending + sorted_vec <- sort(vec) + end_point <- length(vec) - n + if (end_point <= 0) { + return(1) + } + # Exclude n largest (most extreme) values + filtered_vec <- sorted_vec[(1:(length(vec)-n))] + # Find the maximum value among excluded values if any exist + max_value <- max(filtered_vec, na.rm = TRUE) + return(max_value) +} + + +#' Round values to the nearest 0.5 +#' +#' @param x a numeric vector to round +#' +#' @return a numeric vector with all elements rounded to 0, 0.5, or 1 +#' @export +#' +#' @examples +#' round_to_nearest_half(0.9) +#' round_to_nearest_half(0.7) +#' round_to_nearest_half(0.4) +round_to_nearest_half <- function(x) { + if (x %% 1 == 0) { # If x is already an integer, no change needed + return(as.integer(x)) + } else { + decimal_part <- x - floor(x) + if (decimal_part >= 0.25 & decimal_part < 0.75) { + rounded_x <- floor(x) + 0.5 + } else { + rounded_x <- round(x, 0) + } + return(rounded_x) + } +} + + +#' Round values to the nearest 0.5 +#' +#' @inheritParams round_to_nearest_half +#' +#' @return a numeric vector with all elements rounded to 0, 0.5, or 1 +#' @export +#' +#' @examples +#' rnh(c(0.2, 0.3, 0.4, 0.8, 0.09, 0.9)) +rnh <- function(x) { + tmp <- Vectorize(civilytics::round_to_nearest_half) + tmp(x) +} diff --git a/man/rnh.Rd b/man/rnh.Rd new file mode 100644 index 0000000..8b57ad9 --- /dev/null +++ b/man/rnh.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{rnh} +\alias{rnh} +\title{Round values to the nearest 0.5} +\usage{ +rnh(x) +} +\arguments{ +\item{x}{a numeric vector to round} +} +\value{ +a numeric vector with all elements rounded to 0, 0.5, or 1 +} +\description{ +Round values to the nearest 0.5 +} +\examples{ +rnh(c(0.2, 0.3, 0.4, 0.8, 0.09, 0.9)) +} diff --git a/man/round_to_nearest_half.Rd b/man/round_to_nearest_half.Rd new file mode 100644 index 0000000..4de8075 --- /dev/null +++ b/man/round_to_nearest_half.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{round_to_nearest_half} +\alias{round_to_nearest_half} +\title{Round values to the nearest 0.5} +\usage{ +round_to_nearest_half(x) +} +\arguments{ +\item{x}{a numeric vector to round} +} +\value{ +a numeric vector with all elements rounded to 0, 0.5, or 1 +} +\description{ +Round values to the nearest 0.5 +} +\examples{ +round_to_nearest_half(0.9) +round_to_nearest_half(0.7) +round_to_nearest_half(0.4) +} diff --git a/man/trim_max.Rd b/man/trim_max.Rd new file mode 100644 index 0000000..4dbc7d0 --- /dev/null +++ b/man/trim_max.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{trim_max} +\alias{trim_max} +\title{Take the maximum of a number after trimming values} +\usage{ +trim_max(vec, n) +} +\arguments{ +\item{vec}{a numeric vector} + +\item{n}{integer, the number of maximum values to trim before taking the maximum} +} +\value{ +the highest value after removing the highest n values +} +\description{ +Take the maximum of a number after trimming values +} +\examples{ +trim_max(c(10, 10, 10, 9, 8, 7), n = 2) +trim_max(c(10, 10, 10, 9, 8, 7), n = 3) +trim_max(c(10, 10, 10, 9, 8, 7), n = 4) +}