powered by
This function formats numeric vectors with values between 0 and 1 into percentage strings with special specifications. Those specifications are:
0 and 1 are converted to "0%" and "100%" respectively (takes machine precision into account)
all other values < 0.01 are converted to "<1%"
all other values between 0.01 and 0.995 are rounded to percentages without decimals
values between 0.995 and 0.999 are rounded to percentages with 1 decimal
values between 0.999 and 1 are converted to ">99.9%" unless closer to 1 than machine precision.
fmt_pct_special(x)
A character vector
A vector of numerical values
x <- c(0, 0.004, 0.009, 0.011, 0.9, 0.98, 0.994, .995, .9989, .999, .9991, .99999999) fmt <- fmt_pct_special(x) data.frame(x = x, fmt = fmt)
Run the code above in your browser using DataLab