The round() function in R: How To Use Round Function In R?

round() function in r

In this tutorial, you will learn how to use the round() function in R. It can help you round the values of the number, the vector, or the data frame. Let’s follow this article to learn more about it with the explanation and examples below.

Round Function In R

What does round() do in R?

The round() function in R can help you round the values of the number in the first parameter with the specified decimal places in the second parameter. It can round a number, the numbers in the vector, or the numbers in the data frame.

Syntax:

round(object, digits = 0)

Parameters:

  • object: The number, the vector or the data frame.
  • digits: The default is 0. The specified decimal places.

After learning the usage and syntax of the round() function, you will learn how to use it in the next title.

How to use the round() function in R

The round() function in R can help you round a number, the numbers in the vector, and the numbers in the data frame with the specified decimal places. Let’s learn about it below.

Round a number with the round() function

You can round a number with the round() function with the specified decimal places.

Look at the example below.

number = 47.22

# Round the number with zero digits
new_number1 = round(number)

# Round the number with one digit
new_number2 = round(number,1)

print(new_number1)
print(new_number2)

Output

[1] 47
[1] 47.2

Round the numbers in the vector with the round() function

You can round the numbers in the vector with the round() function with the specified decimal places.

Look at the example below.

number_arr = c(1.2, 1.3, 3.85, 4.77, 5.78)

# Round all elements in the vector with zero digits
new_arr1 = round(number_arr)

# Round all elements in the vector with one digit
new_arr2 = round(number_arr, 1)

print(new_arr1)
print(new_arr2)

Output

[1] 1 1 4 5 6
[1] 1.2 1.3 3.9 4.8 5.8

Round the numbers in the data frame with the round() function

You can round the numbers in the data frame with the round() function with the specified decimal places.

Look at the example below.

# Round the total cost with zero digits
payroll = data.frame( 
    Name = c("David", "Peter", "Elsa", "Tiffany", "John"),
    Total_Cost = c(156.67, 289.75, 155.66, 147.23, 200.4) 
)

payroll$Rounded = round(payroll$Total_Cost, 1)
payroll

Output:

     Name Total_Cost Rounded
1   David     156.67   156.7
2   Peter     289.75   289.8
3    Elsa     155.66   155.7
4 Tiffany     147.23   147.2
5    John     200.40   200.4

Summary

You have learned about the usage syntax and how to use the round() function in R. By this function, you can round a number, the numbers in the vector, or the numbers in the data frame. If you have any questions about his function, please leave your comments below. I will answer your questions. Thanks!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *