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.
How to 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.
round(x, digits = 0)
The x is a numeric vector.
The digits argument indicates the number of decimal places (round) or significant digits (signif) to be used.
round(2119.1921, digits = 3)
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)
Adding negative digits argument
You can add the negative number as the digits argument to the round() function.
round(2119.1921, digits = -2)
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)
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.
If we round 1.5, then it will also give the same output.
That is it for the R round() function.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.