View source: R/S03_Utilities.R
percent_that_sums_to_100 | R Documentation |
Function that uses the largest remainder method to ensure that a set of percentages sum to 100% even in the presence of rounding error.
percent_that_sums_to_100(x, digits = 1, freq = FALSE)
x |
A vector of proportions or frequencies that are suppose to sum to 100%. |
digits |
The number of digits to round to. |
freq |
Logical; if |
A vector of percentages that sum to 100%.
x <- c( 9990, 5, 5 )
# Convert to percentage and round
p <- round( 100*x/sum(x), 1 )
# No longer sums to 100% due to rounding error
print( sum(p) )
# Adjust percentages using the
# largest remainder method so
# they sum to 100%
print( percent_that_sums_to_100( p/100 ) )
# Works with frequencies as well
print( percent_that_sums_to_100( x, freq = TRUE ) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.