x <- c(1 / 3, 1 / 3, 1 / 3)
# Default method ensures precise rounding but total might not be 100%.
round_pct(x = x)
sum(round_pct(x = x))
# You can trade off rounding precision for precision on the total with the
# method `"round_sum"`.
round_pct(x = x, method = "round_sum")
sum(round_pct(x = x, method = "round_sum"))
# Vary the number of decimal places, e.g. increase to three.
round_pct(x = x, digits = 3, method = "round_sum")
# Missing values are ignored.
x <- c(1, 2, NA)
round_pct(x = x, digits = 3)
Run the code above in your browser using DataLab