This page will show you how to calculate and format a number as a percentage in R.

## Get Example Data

If you want to follow along with the example below you will need the data that is used. To get this data, install and load the eeptools package and then open the sample data by running the following code in R:

# Install eeptools install.packages("eeptools") # Load eeptools library(eeptools) # Open data data(stulevel)

## Example 1: Create a Number and Format as a Percent in R in Two Steps

This example shows how to format a number as a percent in R. Unlike many statistical software packages, R does not have a data type for percent. Instead, fields containing percent signs are character data. In this example we first create the value that we want to format as a percent by dividing stulevel$attday (the number of days that a student attended school) by 180 (the number of possible days of attendance. This gives us a decimal ranging from 0 to 1. We then convert the decimal to a percent.

# Divide days attended by 180 possible days of attendance and save as attday_percent stulevel$attday_percent <- stulevel$attday/180 # Format as percent rounded to one decimal place stulevel$attday_percent <- paste(round(stulevel$attday_percent*100,digits=1),"%",sep="")

## Example 2: Create a Number and Format as a Percent in R in One Step

In the above example I calculate a value and then format it as a percentage using two separate steps to make it clear what’s going on. It’s possible to do this in one step to save some typing:

```
# Create number and format as percent rounded to one decimal place
stulevel$attday_percent2 <- paste(round((stulevel$attday/180)*100,digits=1),"%",sep="")
```

## Example 3: Format as a Percent if the Denominator is Another Column

In the third example I extend the second example to show how to calculate and format a percent in R if the denominator comes from another field in the data frame rather than a static number.

# Create an example denominator to divide by stulevel$possible_attendance <- 180 # Create number and format as percent rounded to one decimal place stulevel$attday_percent3 <- paste(round((stulevel$attday/stulevel$possible_attendance)*100,digits=1),"%",sep="")

# Practice

To practice formatting data in R, try the exercises in this string manipulation with R tutorial.

Thanks for reading! This website took a great deal of time to create. If it was helpful to you, please show it by sharing with friends, liking, or tweeting! If you have any thoughts regarding this R code please post in the comments.