While collecting some corpus statistics, I came across a handy function in R that formats decimal into percentage expressions:
library(scales) values = c(0.5, 0.01, 1e-4, 0.2734) percent(values) #[1] "50%" "1%" "0%" "27%" # More verbose equivalent using sprintf sprintf("%.0f%%", 100 * values)
The function is clearly most valuable if all values are in a range of 1 to 100 percent, as the documentation also suggests: “Multiply by one hundred and display percent sign.” [1] In contrast, the sprintf equivalent may be ‘configured’ such as to show more decimal places.
On Ubuntu, the enclosing package can be installed via
sudo apt-get install r-cran-scales
References
- [1] scales package documentation
Edits
- 2017-05-03: Use library(scales) instead of library(ggplot2). Thanks to Vital!