round in R: How to Use round() Function to Round Off Numbers

Rounding implies making a number milder but keeping its value nearer to what it was. The result after rounding is less accurate but easier to use. When rounding a number, the first question in your mind should be: what are you rounding it to? The numbers can be rounded to the closest ten, nearest hundred, or thousand, and so on.

How to Round Numbers in Mathematics

To round the numbers, you need to decide which is the last digit to retain. If the next digit is less than 5, then leave it the same, which is called rounding down. If the next digit is 5 or more, it will increase by 1, called rounding up.

round in R

To round a number to specific digits after the decimal point in R, use the round() function. The round() method rounds the values in its first argument to the defined decimal places. The default is 0.

Syntax

round(x, digits = 0)

Parameters

The is a numeric vector.

The digits argument indicates the number of decimal places (round) or significant digits (signif) to be used.

Example

round(2119.1921, digits = 3)

Output

[1] 2119.192

In this example, we are rounding off the numeric vector to the 3 digits. We can round off up to 2 digits bypassing digits = 2.

round(2119.1921, digits = 2)

Output

[1] 2119.19

Adding negative digits argument

You can add the negative number as the digits argument to the round() function.

round(2119.1921, digits = -2)

Output

[1] 2100

It rounded off to 2100.

R signif() function

To define the number of significant digits to be retained despite the number’s size, use the R signif() function.

signif(2119.1921, digits = 4)

Output

[1] 2119

Both round() and signif() functions round numbers to the closest possibility.

If the first digit that’s left is smaller than 5, the number will be rounded down.

If the first digit is bigger than 5, the number will be rounded up.

If the first digit that left precisely to 5, R uses a common rule of programming languages: Always round to the nearest even number.

The round(1.5) and round(2.5) both return 2, for example, and round(-3.5) returns -3.

round(2.5)

Output

[1] 2

If we round 1.5, then it will also give the same output.

round(1.5)

Output

[1] 2

That is it for the R round() function.

See also

Sum in R

Mean in R

Mode in R

R log()

Square root in R

1 thought on “round in R: How to Use round() Function to Round Off Numbers”

  1. This a great feature. Really Cool. Respect an opportunity to get super stats regarding units blogs and forums, too! I serious like everything about this wundabar artical…

    Reply

Leave a Comment