Rounding off the numbers can be achieved with round() in R. Round is one of the best ways to make numerical values more meaningful and to get more precise results in the analysis.

R offers the standard function round() to round off the numbers based on decimal values as well.

Well, in this tutorial we are going to round off the numbers in various aspects. Let’s roll!!!


Starting with the syntax of round() in R

The syntax of the round() function is given below,

where,

  • x -> numerical value or a vector available for round off
  • digits -> This value represents the number of decimal points that you wish to have for the number

Rounding off the numbers using round() in R

Some of you may know that R can accurately calculate up to 16 digits. But you always don’t need those 16 digits for your analysis. So using the round() function in R, you can easily round the numbers with specific decimal points as well.


Round off a single numerical value

Here we are going to round off a single value using the function round().

As the below output shows, the digit value will indicate the decimal points.

Output: 143

Output: 143.1

Output: 143.12


Round Up Values in a Vector

Now let’s round off the values stored in a vector.

Output= 143.24 34.77 789.65 224.57 23.57 9.76

Output= 143.236 34.765 789.654 224.568 23.567 9.760

Output= 143 35 790 225 24 10


Rounding Negative Numbers in R

Sometimes the data may also consist of negative values. Let’s see how we can round off the negative values in a vector.

Output= -143.24 -34.77 -789.65 -224.57 -23.57 -9.76

Output = -143 -35 -790 -225 -24 -10

Note: Keep in mind that if you did not mention the digits, by default it will be considered as 0 as shown above. Also, notice how the process continues to be the same irrespective of the type of integer value specified.


Round off the values in an expression

Using round() in R we can easily round off the numbers in an expression.

The round() function will solve the expression and rounds off the final value with 2 decimal points as shown below.

Output= 602.38


Round values in a dataset using round() in R

We have gone through various aspects of a round() function. Let’s round off the values present in an iris dataset.

Iris dataset view

round-in-r-3

Output= All the values are rounded with 0 decimal points.

Signif() function to round off the numbers

If you wonder, what is signif()? It is like a round function, but it looks for total digits instead of decimal points alone.

Output= 34.6

Here you can observe that the signif() function will round off the total number of digits. It doesn’t deal with the decimal points.

Output= 34.567



Wrapping up

R is very aggressive in data analysis. Round off the values found greater importance in data analysis as it yields good accuracy in the results. With the help of round() function in R, you can easily round off the values with specific decimal points as well.

Try rounding off more values and stay connected for more R tutorials. Happy roundoff!!!

For more study: R documentation/round

By admin

Leave a Reply

%d bloggers like this: