Normalizes weights using different methods with support for outside options.
Arguments
- x
Numeric vector, matrix, or SpatRaster of weights
- method
Character string specifying normalization method:
- "standard": Divide by sum + a0 (default)
- "semi": Normalize only if sum + a0 > 1
- "reference": Normalize by reference value
- "identity": Return unchanged
- Or a custom function(x, ...)
- ref_value
Optional reference value for "reference" method
- a0
Non-negative numeric value representing outside option weight (default = 0)
- snap
Logical; if TRUE, avoid input validation
- ...
Additional arguments passed to custom normalization function
Examples
if (FALSE) { # \dontrun{
library(terra)
# Example 1: Basic vector normalization
weights <- c(2, 3, 5)
calc_normalize(weights) # Returns c(0.2, 0.3, 0.5)
# Example 2: Semi-normalization with vector
small_weights <- c(0.2, 0.3, 0.4)
calc_normalize(small_weights, method = "semi") # Returns original values
large_weights <- c(0.5, 0.7, 0.9)
calc_normalize(large_weights, method = "semi") # Normalizes since sum > 1
# Example 3: Working with raster data
r <- rast(nrows = 10, ncols = 10)
values(r) <- runif(100) # Random weights
norm_rast <- calc_normalize(r) # Values sum to 1
# Example 4: Using outside option (a0)
weights_with_outside <- c(2, 3, 5)
calc_normalize(weights_with_outside, a0 = 10) # Includes outside option weight
# Example 5: Reference normalization
values <- c(10, 15, 25)
calc_normalize(values, method = "reference", ref_value = 20)
# Example 6: Custom normalization function
custom_norm <- function(x) {
x / (max(x, na.rm = TRUE) + min(x, na.rm = TRUE))
}
calc_normalize(values, method = custom_norm)
# Example 7: Multi-layer raster normalization
r_stack <- c(r, r * 2, r * 0.5) # Create 3-layer raster
names(r_stack) <- c("layer1", "layer2", "layer3")
norm_stack <- calc_normalize(r_stack) # Normalizes each cell across layers
# Example 8: Handling NA values
weights_with_na <- c(2, NA, 5, 3)
calc_normalize(weights_with_na) # NA values are handled appropriately
} # }