# Format Number as Percentage in R (3 Examples) | Express Numeric Values in Percent

On this page, I’ll explain how to **express numeric values in percent** in the R programming language.

The post is structured as follows:

- Creating Example Data
- Example 1: Format Number as Percentage with User-Defined Function
- Example 2: Format Number as Percentage with scales Package
- Example 3: Format Number as Percentage with formattable Package
- Video, Further Resources & Summary

Let’s jump right to the R syntax:

## Creating Example Data

The examples of this tutorial are based on the following example vector:

x <- c(1.2, 0.5, 0.103, 7, 0.1501) # Create example vector

Our example vector is called x and contains four different numeric values.

In the following R programming tutorial, I’ll show how to convert these numeric values to percentages. Note that the following R codes are based on Stack Overflow.

## Example 1: Format Number as Percentage with User-Defined Function

The basic installation of the R programming language doesn’t provide a function for the conversion of numbers to percentages. However, we can create our own function for this task as shown below:

percent <- function(x, digits = 2, format = "f", ...) { # Create user-defined function paste0(formatC(x * 100, format = format, digits = digits, ...), "%") }

Now, we can apply our user-defined function as follows:

percent(x) # Apply user-defined function # "120.00%" "50.00%" "10.30%" "700.00%" "15.01%"

As you can see based on the output of the RStudio console, our input vector was formatted into percentages. Note that the output vector has the character class instead of the numeric class.

## Example 2: Format Number as Percentage with scales Package

In Example 1, we created a user-defined function for the expression of numbers as percentages. However, there are multiple add-on packages for the R programming language available, which provide similar functions.

For instance, the scales package does provide such a function. Let’s install and load the scales package to R:

install.packages("scales") # Install and load scales library("scales")

Now, we can use the percent function of the scales package as follows:

scales::percent(x) # percent function of scales # "120.0%" "50.0%" "10.3%" "700.0%" "15.0%"

Note that the percent command of the scales package automatically rounds the output to only one decimal digit. However, you may change that in the options of the functions.

## Example 3: Format Number as Percentage with formattable Package

Another package that provides a function for the conversion of a number to a percentage format is the formattable package. Let’s install and load the package:

install.packages("formattable") # Install and load formattable library("formattable")

The relevant function of the format package is also called percent() (as the function of the scales package shown in Example 2). We can apply the percent function of the formattable package as shown below:

formattable::percent(x) # percent function of formattable # 120.00% 50.00% 10.30% 700.00% 15.01%

As you can see based on the RStudio console output, the percent function of the formattable package displays two digits after the decimal point.

## Video, Further Resources & Summary

Would you like to learn more about the manipulation of data in R? Then you might watch the following video of my YouTube channel. In the video, I illustrate the R programming code of this tutorial in the R programming language:

**Please accept YouTube cookies to play this video.** By accepting you will be accessing content from YouTube, a service provided by an external third party.

If you accept this notice, your choice will be saved and the page will refresh.

In addition, you may want to have a look at the related R articles of my website.

In this article you learned how to **represent a numeric value as percentage** in the R programming language. In case you have further questions, let me know in the comments.

### Statistics Globe Newsletter