Rounding off values in R Language – round() Function

round() function in R Language is used to round off values to a specific number of decimal value.

Syntax: round(x, digits)

Parameters:
x: Value to be round off
digits: Number of digits to which value has to be round off

Example 1: Rounding off the values

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# round functiom
  
# Create example values
x1 <- 1.2                           
x2 <- 1.8
x3 <- - 1.3
x4 <- 1.7
  
# Apply round function
round(x1)                           
round(x2)
round(x3)
round(x4)
  
print(x1, x2, x3, x4)

chevron_right


Output :



 1
 2
-1
 2

Here in the above code, we have round off the values with 4 data x1, x2, x3, and x4 using the function round(), and printed the new values.

Example 2: Rounding off to certain digits

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# round to a certain digit
  
# Create numeric with many digits 
x2 <- 3.14734343243   
  
# Round to three decimal places                  
round(x2, digits = 3)  
  
print(x2)             

chevron_right


Output:

 3.147 

Here in the above code, the value of a numeric vector is round off till 3 digits.

My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.