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,
1 |
round(x,digits=0) |
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.
1 |
round(143.1234, digits = 0) |
Output: 143
1 |
round(143.1234, digits = 1) |
Output: 143.1
1 |
round(143.1234, digits = 2) |
Output: 143.12
Round Up Values in a Vector
Now let’s round off the values stored in a vector.
1 2 3 4 |
#creates a vector with multiple numerical values in it df<-c(143.236,34.765,789.654,224.568,23.567,9.76) #round off the number in a vector with decimal point 2 round(df, digits = 2) |
Output= 143.24 34.77 789.65 224.57 23.57 9.76
1 2 3 |
df<-c(143.236,34.765,789.654,224.568,23.567,9.76) #round off the number in a vector with decimal point 3 round(df, digits = 3) |
Output= 143.236 34.765 789.654 224.568 23.567 9.760
1 2 3 |
df<-c(143.236,34.765,789.654,224.568,23.567,9.76) #round off the number in a vector with decimal point 0 round(df, digits = 0) |
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.
1 2 |
df<-c(-143.236,-34.765,-789.654,-224.568,-23.567,-9.76) round(df, 2) |
Output= -143.24 -34.77 -789.65 -224.57 -23.57 -9.76
1 2 |
df<-c(-143.236,-34.765,-789.654,-224.568,-23.567,-9.76) round(df) |
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.
1 2 3 4 |
#creats an expression my_expression<- c(34.567+234.6789-234.67+567.8) #round off the values in an expression round(my_expression) |
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.
1 2 |
df<- datasets::iris df |
Iris dataset view
1 |
round(iris$Sepal.Length,0) |
Output= All the values are rounded with 0 decimal points.
1 2 3 4 5 |
[1] 5 5 5 5 5 5 5 5 4 5 5 5 5 4 6 6 5 5 6 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 5 5 [37] 6 5 4 5 5 4 4 5 5 5 5 5 5 5 7 6 7 6 6 6 6 5 7 5 5 6 6 6 6 7 6 6 6 6 6 6 [73] 6 6 6 7 7 7 6 6 6 6 6 6 5 6 7 6 6 6 6 6 6 5 6 6 6 6 5 6 6 6 7 6 6 8 5 7 [109] 7 7 6 6 7 6 6 6 6 8 8 6 7 6 8 6 7 7 6 6 6 7 7 8 6 6 6 8 6 6 6 7 7 7 6 7 [145] 7 7 6 6 6 6 |
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.
1 |
signif(34.567, 3) |
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.
1 |
signif(34.567, 5) |
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