Skip to content
Related Articles

Related Articles

Improve Article

Format Number as Percentage in R

  • Last Updated : 23 May, 2021
Geek Week

In this article, we are going to see how to format numbers as percentages in R programming language.

Method 1: Using formattable package

The “formattable” package provides methods to create formattable vectors and data frame objects. This package in R can be installed and loaded into the working space using the command :

install.packages("formattable")

The percent() method in this package is used to represent the numerical vectors to percentage format. 

Syntax: percent(vec, digits, format = “f”, …)



Arguments : 

  • vec – The vector to convert to percentage format
  • digits – The digits to have after decimal point. By default, the number of digits is equivalent to 2.
  • format – format type string passed to the method format.

Code:

R




# loading the required libraries
library("formattable")
  
# creating a vector
vec <- c(0.76485, 1.34, -0.6, 1)
  
print ("Percentage conversion : 2 digits")
percent (vec)
  
print ("Percentage conversion : 4 digits")
percent (vec, 4)

Output:

[1] "Percentage conversion : 2 digits"
[1] "76.48%"  "134.00%" "-60.00%" "100.00%"
[1] "Percentage conversion : 4 digits"
[1] "76.4850%"  "134.0000%" "-60.0000%" "100.0000%"

Method 2: Using scales package

The “scales” package in R can be installed and loaded into the working space using the command : 

install.packages("scales")

The percent() method in this package is used to represent the numerical vectors to percentage format.

Syntax: percent(vec)



Arguments : vec – The vector to convert to percentage format

Code:

R




# loading the required libraries
library("scales")
  
# creating a vector
vec <- c(0.76485, 1.34, -0.6, 1)
  
print ("Percentage conversion")
percent (vec)

Output:

[1] "Percentage conversion" 
[1] "76%"  "134%" "-60%" "100%"

Method 3: Using user-defined function

A user-defined method can be used to convert a numerical to a percentage format. The format specifier used in this function is “f” and the number of digits can also be taken as an input to specify the number of integers after the decimal point. The integer number is multiplied by 100 and then the formatC method is applied until the number of digits. In the end, using the paste0() method, the “%” sign is appended at the end out output.

R




# creating a function to compute percentage
percent <- function(num, digits = 2, ...) {      
  percentage <-formatC(num * 100, format = "f", digits = digits, ...)
    
  # appending "%" symbol at the end of
  # calculate percentage value
  paste0(percentage, "%")
}
  
# defining a vector
vec <- c(0.76485, 1.34, -0.6, 1, 0.0284)
print ("Percentage conversion to three decimal places")
  
# rounding off the numbers to 3 places
# of decimal
percent(vec, 3)
  
print ("Percentage conversion to two decimal places")
  
# rounding off the numbers to 2 places 
# of decimal
percent(vec)

Output

[1] "Percentage conversion to three decimal places" 
[1] "76.485%"  "134.000%" "-60.000%" "100.000%" "2.840%"   
[1] "Percentage conversion to two decimal places"
[1] "76.48%"  "134.00%" "-60.00%" "100.00%" "2.84%"  



My Personal Notes arrow_drop_up
Recommended Articles
Page :