Skip to content
Related Articles

Related Articles

Rounding off values in R Language – round() Function
  • Last Updated : 26 May, 2020

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 Articles
Page :