Rounding in R (4 Examples) | round, ceiling, floor, trunc & signif Functions

 

This tutorial explains how to round numbers in R with the functions round(), ceiling(), floor(), trunc(), and signif().

The article shows in four examples how the different rounding functions are used in the R programming language.

Sound good? Let’s dive right in…

 

Example 1: Comparison of round, ceiling, floor, trunc & signif

The R programming language provides many different rounding functions, which are all slightly different.

Figure 1 illustrates based on four different input values (i.e. 1.1, 1.9, – 1.1, and – 1.9) how the output of the different functions looks like.

Values in red cells were rounded down to the next lower number and values in green cells were rounded up to the next higher numbers.

 

Round Data in R

Figure 1: Comparison of the round, ceiling, floor, trunc & signif R Functions.

 

Definition of round R function: The round function rounds a numeric input to a specified number of decimal places.

Definition of ceiling R function: The ceiling function rounds a numeric input up to the next higher integer.

Definition of floor R function: The floor function rounds a numeric input down to the next lower integer.

Definition of trunc R function: The trunc function truncates (i.e. cuts off) the decimal places of a numeric input.

Definition of signif R function: The signif function rounds a numeric input to a specified number of digits.

 

In the following, you can find the R code that computes the values of Figure 1. First, we need to create numeric data objects that we can use in the following rounding examples:

x1 <- 1.1                            # Create example values
x2 <- 1.9
x3 <- - 1.1
x4 <- - 1.9

Now, we can apply each of the five functions to these four example values. First, we apply the round function, …

round(x1)                            # Apply round function
round(x2)
round(x3)
round(x4)

…then we apply the ceiling function, …

ceiling(x1)                          # Apply ceiling function
ceiling(x2)
ceiling(x3)
ceiling(x4)

…the floor function, …

floor(x1)                            # Apply floor function
floor(x2)
floor(x3)
floor(x4)

…the trunc function, …

trunc(x1)                            # Apply trunc function
trunc(x2)
trunc(x3)
trunc(x4)

…and the signif function:

signif(x1, digits = 1)               # Apply signif function
signif(x2, digits = 1)
signif(x3, digits = 1)
signif(x4, digits = 1)

As you can see, the R syntax of the five functions is very similar. However, there are plenty of situations where we need to adjust the specifications of the rounding functions. So keep on reading…

 

Example 2: Round to Certain Amount of Digits

In the previous example, we rounded our data to zero decimal places. However, the round and signif functions can be used to round to a certain amount of decimal places.

Consider the following example number:

x5 <- 1.23456789                     # Create numeric with many digits

We can use the round function to round this number to, for instance, five decimal places:

round(x5, digits = 5)                # Round to five decimal places
# 1.23457

Or we can use the signif command to round our example number to five digits:

signif(x5, digits = 5)               # Round to five digits
# 1.2346

Note: The difference between round and signif is that round allows to specify the number of decimal places and signif allows to specify the number of digits (i.e. places before AND after the decimal point).

 

Example 3: Round to Nearest 10

The round function can be used to round to the next 10 (i.e. 10, 20, 30, 40…). Consider the following numeric value:

x10 <- 77                            # Create number with two digits

We can round this numeric value (i.e. the number 77) to the closest 10 by specifying a negative integer to the digits option of round:

round(x10, digits = - 1)             # Round to nearest 10
# 80

Note that this principle can also be applied in order to round to the next 100, 1000, 10000, and so on. Simply increase the negative specification of digits.

 

Example 4: Round .5 Up to Next Higher Value

The round function rounds .5 (e.g. 0.5, 7.5, 10.5 etc.) to the next even number. This can be illustrated with the following example.

Rounding 1.5 to zero decimal places results in the value 2 (i.e. .5 is rounded upwards):

round(1.5)                           # Round up to next even value
# 2

However, rounding 2.5 results in the value 2 as well (i.e. .5 is rounded downwards):

round(2.5)                           # Round down to next even value
# 2

If we always want to round .5 up, we need to create our own function. The following user-defined function always rounds .5 upwards (note: the function is based on a code that I found here):

round2 <- function(x, digits = 0) {  # Function to always round 0.5 up
  posneg <- sign(x)
  z <- abs(x) * 10^digits
  z <- z + 0.5
  z <- trunc(z)
  z <- z / 10^digits
  z * posneg
}

Let’s apply our new function to 1.5 and 2.5 (as we did before with the original round function):

The value 1.5 is rounded to 2…

round2(1.5)                          # Apply new rounding function
# 2

…and the value 2.5 is rounded to the odd number 3:

round2(2.5)                          # Apply new rounding function
# 3

 

Tutorial Video & Further Resources for the Formatting of Numeric Data in R

In case you need further explanations on the code of this page, you may check out the following YouTube video on the Statistics Globe YouTube channel. It explains the syntax of this article in some more detail:

 

 

If you need other examples for the different rounding functions, I can recommend the following video of the R-programming Library YouTube channel:

 

 

In addition, you could have a look at some of the other R programming tutorials on this website. I’m publishing new R tutorials on a regular basis:

This article explained how to round numeric data in R.

Note that we have used simplified examples in this tutorial. However, it would be possible to apply the same kinds of R syntaxes to complex vectors or data frame columns as well.

In any case, if you have any further questions or comments, let me know in the comments section below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


8 Comments. Leave new

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top