Skip to contents

Calculates the percentile ranking of a value within a vector of values. This function was moved from artbenchmark to artutils to respect the package dependency hierarchy.

Usage

calc_percentile(val, vec, trim = c(0, 1))

Arguments

val

Numeric. Single value to calculate percentile for.

vec

Numeric vector. Values to compare against.

trim

Numeric vector of length 2. Quantile range to keep (default: `c(0, 1)` keeps all values). Use to exclude outliers (e.g., `c(0.05, 0.95)`).

Value

Numeric. Percentile ranking from 0 to 100.

Details

Percentile calculation uses the rank-based method: - 1 = value is below the minimum (outside range) - 0.5 = value equals the minimum - 50 = value is at the median - 99.5 = value equals the maximum - 100 = value is above the maximum (outside range)

The trim parameter allows you to exclude extreme values from the comparison vector before calculating the percentile.

Note

This function was previously exported by artbenchmark. It was moved to artcore to maintain proper dependency hierarchy: artbenchmark depends on artutils depends on artcore.

See also

Other utilities: shared-utils

Examples

# Basic usage
calc_percentile(25, 1:100) # Returns 24.5
#> [1] 24.5
calc_percentile(75, 1:100) # Returns 74.5
#> [1] 74.5

# Edge cases
calc_percentile(1, 1:100) # Returns 0.5 (at minimum)
#> [1] 0.5
calc_percentile(100, 1:100) # Returns 99.5 (at maximum)
#> [1] 99.5
calc_percentile(0, 1:100) # Returns 1 (below minimum)
#> [1] 1
calc_percentile(150, 1:100) # Returns 100 (above maximum)
#> [1] 100