R round Function

The R round method is one of the Math functions, which round a specific number or an expression to the nearest value. Let us see how to use round in R Programming language with an example.

R round syntax

The syntax of the round in R Programming language is

# Round numeric_Expression to nearest value 
round(numeric_Expression)

# Round the decimal points to specified integer_value 
round(numeric_Expression, integer_value)

Numeric_Expression: It can be a numeric value or a valid numerical expression.

  • If the numeric_Expression is positive or Negative numeric value, it returns the nearest ceiling value.
  • If it is not a number (NaN), then it returns NaN. And if it is positive or negative infinity, the function returns the same.

R round Function example

In this program, We are going to return the round values of different data and display the output

# Use on Positive  Value
round(45.56)
round(525.4999)

# Use on Negative values
round(-140.825)
round(-13.23)

# Expression
round(140.986 + 122.456 - 220.4233 + 12.67)

# on vectors
number <- c(-22.26, 456.94, 2.50, 2.51, -36.49, -813.111 , -525.123)
round(number)
R Round Function 1

R round Function example 2

In this Program, We are going to round the decimal values of different data & display the output

# Use on Positive  Value
round(45.565, 0)

# decimal points to 1 
round(525.419299, 1)
round(525.419299, 2)

# Use on Negative values
round(-140.825, 2) 
round(-13.239, 2)

# Expression
round((140.986 + 122.4563256 - 220.4233 + 12.67), 2)

# vectors
number <- c(-22.26, 456.94, 2.50, 2.591, -36.49, -813.111 , -525.123)
round(number, 1)
R Round Function 2

R round Function example 3

In this program, We are going to apply the round function on List data and display the output. For this example, we are using the airquality data set provided by R

#Dataset We are going to use
airquality

# Applying on Airquality Wind Data
round(airquality$Wind)

round(airquality$Wind, 1)
R Round Function 3

This function in R programming also allows you to round the numeric values in a database or table columns. In this example, We are going to round all the records present in [Standard Cost] and [Sales Amount] columns. For this, We are going to use the below-shown CSV file data.

TIP: Please refer to the R Read CSV Function article to understand the steps involved in importing the CSV file.

R round Function 5

R CODE

getwd()

#Datat We are going to use
product <- read.csv("Product_Data.csv", TRUE, sep = ",")
print(product)

# Applying on Standard Cost, and Sales Amount
round(product$StandardCost)
round(product$StandardCost, 1)

round(product$SalesAmount)
round(product$SalesAmount, 2)
R Round Function 4