add rounding utilities
Gitea Organization/civilyticsR/pipeline/head There was a failure building this commit
Gitea Organization/civilyticsR/pipeline/head There was a failure building this commit
This commit is contained in:
+1
-1
@@ -1,7 +1,7 @@
|
||||
Package: civilytics
|
||||
Type: Package
|
||||
Title: Utilities Functions for Civilytics
|
||||
Version: 0.1.0
|
||||
Version: 0.2.0
|
||||
Author: Jared E. Knowles <jared@civilytics.com>
|
||||
Maintainer: Jared E. Knowles <jared@civilytics.com>
|
||||
Description: House R functions for Civilytics Consulting LLC
|
||||
|
||||
Vendored
+3
-3
@@ -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
|
||||
'''
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
+20
@@ -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))
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user