Formatting numbers as percentages is a common task when working with numeric data in R, especially when the goal is to present results in a more readable and understandable manner. This is a versatile skill that can significantly enhance the presentation of your data, making it more accessible to varied audiences. In this comprehensive guide, we’ll explore different methods for achieving this in R.
Percentages represent a part of a whole and are a way to express fractions, ratios, or proportions out of 100. Representing numbers as percentages can make data more relatable and easily comparable, particularly when working with varying scales or magnitudes.
1. Basic Formatting using scales Library
scales library in R provides a convenient
percent() function to format numeric vectors as percentages effortlessly. The
percent() function is versatile, allowing for adjustments like controlling the number of decimal places.
library(scales) values <- c(0.123, 0.555, 0.879) # Formatting with one decimal place formatted_values_one_decimal <- percent(values, accuracy = 0.1) # Formatting with two decimal places formatted_values_two_decimals <- percent(values, accuracy = 0.01)
formatted_values_one_decimal would hold “12.3%”, “55.5%”, “87.9%”, and
formatted_values_two_decimals would hold “12.30%”, “55.50%”, “87.90%”.
2. Application in DataFrames:
Converting numerical values to percentages within a dataframe can be achieved by applying the
percent function directly to a dataframe column.
library(scales) library(dplyr) df <- data.frame( Value = c(0.123, 0.555, 0.879) ) df <- df %>% mutate(Percentage_One_Decimal = percent(Value, accuracy = 0.1), Percentage_Two_Decimals = percent(Value, accuracy = 0.01)) print(df)
Value Percentage_One_Decimal Percentage_Two_Decimals 1 0.123 12.3% 12.30% 2 0.555 55.5% 55.50% 3 0.879 87.9% 87.90%
In this revised dataframe,
Percentage_One_Decimal column will contain percentages with one decimal place, and
Percentage_Two_Decimals column will contain percentages with two decimal places.
3. Custom Formatting using sprintf :
sprintf() function is valuable when a customized format string is desired, providing flexibility in defining the format of the resulting string.
values <- c(0.123, 0.555, 0.879) formatted_values_one_decimal <- sprintf("%.1f%%", values * 100) formatted_values_two_decimals <- sprintf("%.2f%%", values * 100)
4. Using knitr for Table Outputs:
kable function from the
knitr package is excellent for representing data in tables, with columns containing percentages being formatted using the
library(knitr) library(scales) df <- data.frame(Values = c(0.123, 0.555, 0.879)) df$Values <- percent(df$Values, accuracy = 0.1) kable(df)
|Values | |:------| |12.3% | |55.5% | |87.9% |
5. Advanced Formatting with formattable :
formattable package provides a plethora of formatting options, including formatting percentages, for enhancing the representation of data frames and tables.
library(formattable) values <- c(0.123, 0.555, 0.879) formatted_values <- percent(values, digits = 2) # For two decimal places print(formatted_values)
 12.30% 55.50% 87.90%
Understanding how to appropriately format numbers as percentages in R is pivotal for data analysis and presentation. By using the
percent function and manipulating the
accuracy parameter or employing other methods such as
sprintf or the
formattable package, users can attain optimal, tailored representations of percentages, aiding in conveying clearer, more precise insights. Whether embedded within data frames, represented in tables using
knitr, or customized using
sprintf, well-formatted percentages elevate the clarity and comprehensibility of your data presentations and communications.