Convert Numbers into Percentages in R with examples

Convert Numbers into Percentages in R, The percent() function from the scales package is the simplest way to format values as percentages in R. The syntax for this function is as follows.

Convert Numbers into Percentages in R

percent(x, accuracy = 1)

where:

x: The object to format as a percentage.
accuracy: A figure to round up to. Use.01 to round to two decimal places, for example.

This tutorial shows you how to use this function in practice with various examples.

Example 1: Format Percentages in a Vector

The following code demonstrates how to format numbers in a vector as percentages.

Wilcoxon Signed Rank Test in R » an Overview » finnstats

library(scales)

Let create some data

data <- c(.25, .35, .1, .14, .72, .98)
data
[1] 0.25 0.35 0.10 0.14 0.72 0.98

Now we can format numbers as percentages

percent(data, accuracy = 1)
[1] "25%" "35%" "10%" "14%" "72%" "98%"

Numbers should be formatted as percentages with one decimal place.

percent(data, accuracy = 0.1)
[1] "25.0%" "35.0%" "10.0%" "14.0%" "72.0%" "98.0%"

Numbers should be formatted as percentages with two decimal places.

percent(data, accuracy = 0.01)
[1] "25.00%" "35.00%" "10.00%" "14.00%" "72.00%" "98.00%"

Example 2: Format Percentages in a Data Frame Column

The following code demonstrates how to format numbers as percentages in a data frame column.

Normality Test in R » How to Perform » Easy Steps » finnstats

library(scales)

Let’s create a data frame

df1 <- data.frame(Name=c('Q1', 'Q2', 'Q3'),
                  Score=c(44, 66, 77))

View the data frame

df1
   Name Score
1   Q1  0.44
2   Q2  0.66
3   Q3  0.77

In the growth column, format the values as percentages.

df1$Score <- percent(df1$Score, accuracy=1)

View updated data frame

df1
  Name Score
1   Q1   44%
2   Q2   66%
3   Q3   77%

Example 3: Format Percentages in Multiple Data Frame Columns

The following code demonstrates how to format numbers in many columns of a data frame into percentages.

Equality of Variances in R-Homogeneity test-Quick Guide » finnstats

library(scales)

create a data frame

df<- data.frame(Name = c('A', 'B', 'C', 'D'),
                value = c(.28, .3, .4, .58),
                score = c(.56, .45, .77, .35))

Now we can view the data frame

df
   Name value score
1    A  0.28  0.56
2    B  0.30  0.45
3    C  0.40  0.77
4    D  0.58  0.35

In the growth and trend columns, format numbers as percentages.

How to Calculate Mean Absolute Percentage Error (MAPE) in R » finnstats

df[2:3] <- sapply(df[2:3], function(x) percent(x, accuracy=1))
df
  Name value score
1    A   28%   56%
2    B   30%   45%
3    C   40%   77%
4    D   58%   35%
Have you found this article to be interesting? I'd be glad if you could forward it to a friend or share it on Twitter or Linked In to help it spread.

You may also like...

1 Response

  1. Sinyor says:

    Please sir I need help on how to find full book of R charts codes everything on how to creat and controls charts

Leave a Reply

Your email address will not be published. Required fields are marked *

9 − 8 =