Round Function in R

Round function in R, rounds off the values in its first argument to the specified number of decimal places. Round() function in R rounds off the list of values in vector and also rounds off the column of a dataframe. It can also accomplished using signif() function. Let see an example of each.

  • Round() function to round off the values of a vector.
  • round off the values of vector using signif() function in R
  • round off a column in R dataframe using round() function
  • round off the values of column in dataframe using signif() function

 

Syntax of Round() function in R:

round(x, digits = 0)
  • x – numeric value or vector to be rounded off
  • digits – number of digits to which the value has to be rounded off.

Example of Round function in R with two digits: 

In the below example round() function takes up value and 2 as argument. which rounds off the value to two decimal places

# round off in R with 2 decimal places - with R round function

round(125.2395,2)
output:
[1] 125.24

 

Example of Round function with zero digit: 

In the below example round() function takes up value and 0 as argument. which rounds off the value without any decimal place

# round off in R with 0 decimal places - with R round function

round(125.2395, digits=0)
output:
[1] 125

 

 

Example of Round function for vector:

In the below example round() function takes up a vector and 2 as argument. which rounds off the values of a vector to two decimal places

# round function in R with 2 decimal places for a vector

round(c(1.234,2.342,4.562,5.671,12.345,14.567),digits = 2)
output:
[1] 1.23  2.34  4.56  5.67 12.35 14.57

 

 

Example of Signif() function for rounding off:

Signif() function is similar to round function, but digits argument in signif() function denotes the total digits, not the digits after decimal.

# signif function in R with total of 4 digits

signif(125.2395, digits=4)
output:

digits argument is 4 which means the total number of digits will be 4

[1] 125.2

other way of rounding off numbers in r are Truncate, floor and ceiling.

 

 

Example of Round() function in R of the dataframe column:

First lets create a dataframe

#### Create dataframe in R

my_basket = data.frame(ITEM_GROUP = c("Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable","Vegetable","Dairy","Dairy","Dairy","Dairy","Dairy"), 
                       ITEM_NAME = c("Apple","Banana","Orange","Mango","Papaya","Carrot","Potato","Brinjal","Raddish","Milk","Curd","Cheese","Milk","Paneer"),
                       Price = c(100.981,80.643,80.223,90.3,65.3,71.9,62.2,71.3,25.1,62.9,41.9,35.7,50.9,121.7))

my_basket
 

so the resultant dataframe will be

round function in R 11

 

Example of Round function of dataframe with zero digit: 

In the below example round() function is applied to the column of a dataframe in which it takes up the column value and 0 as argument. which rounds off the column value without any decimal place

# round off in R with 0 decimal places - with R round function

my_basket$Rounded = round(my_basket$Price)
my_basket

so the resultant dataframe will be

round function in R 12

 

 

Example of Round function of dataframe in R with two digit: 

In the below example round() function is applied to the column of a dataframe in which it takes up the column value and two as argument. which rounds off the column value to two decimal places

# round off in R with 2 decimal places - with R round function

my_basket$Rounded = round(my_basket$Price,2)
my_basket

so the resultant dataframe will be

round function in R 13

 

 

Round of dataframe column in R using signif(): 

In the below example signif() function is applied to the column of a dataframe in which it takes up the column value and 4 as argument. which rounds off the column value to 4 values. digits argument in signif() function denotes the total digits, not the digits after decimal.

# round off in R using signif() function

my_basket$Rounded = signif(my_basket$Price,4)
my_basket

so the resultant dataframe will be

round function in R 14

 

round function in r                                                                                                          round function in R