Best Ways To Round in R – Round, Signif, Trunc, Ceiling, and Floor (with examples)

The nice thing about R is it can handle some pretty big numbers. Or some very precise numbers, with plenty of digit(s) behind the decimal place. But what goes a-round, comes a-round. For those with normal humans for bosses, clients or professors… we offer our guide on how to round numbers in R.

Rounding numbers in R – 5 Different Ways and How They Work

While each of the five rounding functions we’re going to look at will round your result to the nearest number, they differ in the details. This is generally in two ways: the number of decimal places you are rounding to and how the rounding function handles fractional part in between decimal places.

Floor and Ceiling are the simplest rounding functions. They take a number and round it to the nearest integer. They don’t split hairs either… floor will round down, ceiling will always round up. The result is always the nearest integer. (Relative to other methods, this returns the largest integer for a positive value).

For floating point numbers, round is the most “canonical” version of the rounding function, in terms of being similar to rounding functions from other languages. Take number and a digits argument representing a specific number of decimal places. Return a rounded number with the correct number of decimal places. Fractional digit rounding is handled by assessing the nearest value and rounding up or down appropriately (around the midpoint, effectively).

Trunc has a brutal efficiency to its operations. Take number, specify the number of decimal places you want. Trunc chops off any detail after that. It’s the meat cleaver of rounding functions.

Signif is a more refined approach to rounding a decimal number and is especially useful if you’re creating reports or presetnation materials. Signif uses the number of significant digit(s), not decimal places. This includes numbers both before and after the decimal point. For example, 45.342 and 4534.2 both have the same number of significant digit(s)… and can be formatted similarly on a data table or chart label via this rounding function.

Function: Round in R

Round is an R rounding function with the format of round(number,digits) that rounds the number to the number of digits provided. Here is an example of the round function in action.

# round in R example
> round(1234.56789,3)
 [1] 1234.568
> round(1234.56789,0)
 [1] 1235
> round(1234.56789,-3)
 [1] 1000

A positive number of digits produce that number of digits after the decimal point up to three. However, a negative number of digits zeros out that number of digits before the decimal point.

Function: Signif in R

Signif is an R rounding function with the format of signif(number,digits) and it rounds the number to the number of digits provided. Here is an example of the Signif function in action.

# signif in R example
> signif(1234.566789,4)
 [1] 1235
> signif(1234.566789,7)
 [1] 1234.567
> signif(1234.566789,1)
 [1] 1000

The signif function round to the given number digits. Both the round and signif functions use standard rounding conventions.

Function: Floor in R

Floor is an R rounding function with the format of floor(number) and it rounds the number to the nearest integer that is less than its value. Here is an example of the floor function in action.

# floor in r example 
> floor(3.14159)
 [1] 3
> floor(-3.14159)
 [1] -4

As you can see when you insert pi into the floor function, the result is 3, however, when you insert -pi you get -4. This is the way this function works and why it is called “floor.”

Function: Ceiling in R

Ceiling is an R rounding function with the format of ceiling(number) that rounds the number to the nearest integer that is greater than its value. Here is an example of the ceiling function in action.

# ceiling in R example 
> ceiling(3.14159)
 [1] 4
> ceiling(-3.14159)
 [1] -3

As you can see when you insert pi into the ceiling function, the result is 4. On the other hand, when you insert -pi you get -3. This is the way this function works and why it is called “ceiling.”

Function: Trunc in R

Trunc is an R rounding function with the format of trunc(number) that rounds to the whole number part of the number. Here is an example of the trunc function in action.

#Trunc in R example 
> trunc(3.14159)
 [1] 3
> trunc(-3.14159)
 [1] -3

Note that when you insert pi into the trunc function that you get 3 and that inserting -pi produces -3. This makes it the perfect function for obtaining the integer part of any number.

Applications

Rounding numbers has numerous applications simply because many functions do not produce nice neat results. R’s rounding functions do more than round individual numbers, for example, they can round the entire vector sequences. They also can round an entire R data frame in a single step. The main limit in rounding a complete R data frame is that the rounding functions to only work on numeric values. The solution to this problem is to use the cbind function on the numeric values first, round them, and then use the cbind function on the numeric portion with the text portion. Another limit is that the chart data needs to be complete in all columns or it will produce an error message. When used properly, these R rounding tools are very useful and easy to use.

Rounding numbers is an important part of presenting data meaningfully. In most cases, calculated values do not have any meaning beyond two or three digits so getting rid of the extra digits does not reduce actual precision.

Scroll to top
Privacy Policy