• Post author:
• Post category:R Programming

The `round()` function in R is used to round numbers to a specified number of decimal places or the nearest integer. It is versatile and can be applied to positive and negative numbers, as well as vectors. This article will explain how the `round()` function works in various scenarios, including rounding positive and negative numbers, rounding to specific decimal places, and rounding to the nearest tens or hundreds.

Key Points-

• The `round()` function is used to round numeric values to the nearest integer or to a specified number of decimal places.
• When rounding positive numbers, the `round()` function rounds up or down depending on which integer is closest.
• For negative numbers, `round()` also rounds to the nearest integer, considering the direction towards zero or away from it based on the fractional part.
• If the `digits` argument is not specified, `round()` defaults to rounding to the nearest integer.
• You can round a number to the desired number of decimal places by specifying a positive value.
• Using a negative `digits` value will round the number to the nearest power of ten, such as tens, hundreds, etc.
• The `round()` function can be applied to vectors, rounding each vector element individually.
• This function is versatile and works with individual numbers as well as vectors, making it useful for data formatting and numerical analysis.

## R round() Function

In R, the round() function is a built-in function that rounds the values of its first argument to the specified number of decimal places (default 0).  By using the `digits` parameter, you can control the rounding precision: positive values of `digits` specify the number of decimal places, while negative values round to the nearest power of 10 (e.g., tens, hundreds).

### The syntax of the round() function

Following is the syntax of the `round()` function.

``````
# Syntax of the round() function
round(x, digits = 0)
``````

### Parameters

This function allows two parameters.

• `x`: The number or numeric vector to be rounded.
• `digits`: Specifies the number of decimal places to round to(default value is 0). When set to a positive number, it rounds to the specified number of decimal places; when set to a negative number, it rounds to the nearest power of 10 (e.g., -2 rounds to the nearest hundred). If `digits` is not specified, it rounds to the nearest integer.

### Return Value.

It returns the number rounded off to the specified number of decimal places.

## Round a Positive Number to the Nearest Integer

To round a positive number to the nearest integer, you can use the `round()` function without specifying a `ndigits` parameter. This will automatically round the number to the nearest integer.

``````
# Round a positive number to nearest integer
num <- 3.6
print("Given positive number:")
print(num)
print("After rounding a positive number to the nearest integer")
print(round(num))
``````

The code returned `4` because `3.6` is closer to 4 than 3, so the output is 4.

Yields below output.

## Round a Negative Number to the Nearest Integer

To round a negative number to the nearest integer, you can use the `round()` function without specifying `ndigits`. This will automatically round the number to the nearest integer.

``````
# Round a negative Number to Nearest Integer
num <- -2.3
print("Given negative number:")
print(num)
print("After rounding a negative number to the nearest integer")
print(round(num))
``````

The code returned `-2` because `-2.3` is closer to `-2` than to `-3`.

Yields below output.

## Round a Number to a Specified Decimal Place

Alternatively, you can round a number to a specified number of decimal places by using the `digits` parameter with a particular value. By providing the number and the `digits` parameter to the function, it will round the number to the desired number of decimal places.

``````
# Round a number to a specified decimal place
num <- 5.678
print("Given number:")
print(num)
print("After Rrounding a number to two decimal places")
print(round(num, digits = 2))

# Output:
# [1] "Given number:"
# [1] 5.678
# [1] "After Rrounding a number to two decimal places"
# [1] 5.68
``````

`5.678` is rounded to two decimal places using `round(num, digits = 2)`. The third decimal place (8) is greater than 5, so the second decimal place is rounded up, resulting in `5.68`.

## Round a Negative Number to a Specified Decimal Place

In this example, you can round a negative number to a specific number of decimal places by using the `digits` parameter with a specified value. Provide both the number and the `digits` parameter to the function to achieve the desired rounding accuracy.

``````
# Round a negative number to specified decimal place
num <- -4.557
print("Given number:")
print(num)
print("After Rrounding a negative number to one decimal place")
print(round(num, digits = 1))

# Output:
# [1] "Given number:"
# [1] -4.557
# [1] "After Rrounding a negative number to one decimal place"
# [1] -4.6
``````

Here, the negative number `-4.557` is rounded to one decimal place. The second decimal place (5) causes the first decimal place to be rounded up, resulting in `-4.6`.

## Round a Vector of Positive Numbers to Nearest Integer

To round a vector of positive numbers to the nearest integer, you can pass the vector to the `round()` function. It will round each value in the vector up or down to the nearest integer, depending on which integer is closest.

``````
# Round a Vector of Numbers
vec <- c(2.3, 3.8, 4.6)
print("Given number:")
print(vec)
print("After Rrounding a vector of numbers:")
print(round(vec))

# Output:
# [1] "Given vector:"
# [1] 2.3 3.8 4.6
# [1] "After Rrounding a vector of numbers:"
# [1] 2 4 5
``````

The vector `c(2.3, 3.8, 4.6)` is rounded, resulting in `2`, `4`, and `5` respectively.

## Round a Vector of Negative Numbers to the Nearest Integer

To round a vector of negative numbers to the nearest integer, you can pass the vector to the `round()` function. It will round each value in the vector up or down to the nearest integer, depending on which integer is closest.

``````
# Round a vector of negative numbers
vec <- c(-1.7, -2.5, -3.9)
print("Given vector:")
print(vec)
print("After rounding a vector of negative numbers:")
print(round(vec))

# Output:
# [1] "Given vector:"
# [1] -1.7 -2.5 -3.9
# [1] "After rounding a vector of negative numbers:"
# [1] -2 -2 -4
``````

The results are `-2`, `-2`, and `-4`, respectively.

## Round a R Vector of Mixed Numbers to the Nearest Integer

This example demonstrates rounding a vector of mixed positive and negative numbers to the nearest integer. Let’s pass the vector of mixed numbers into this function to round each value to its nearest integer.

``````
# Round a vector of mixed numbers
vec <- c(-7.4, 4.5, 9.7)
print("Given vector:")
print(vec)
print("After rounding a vector of mixed numbers:")
print(round(vec))

# Output:
# [1] "Given vector:"
# [1] -7.4  4.5  9.7
# [1] "After rounding a vector of mixed numbers:"
# [1] -7  4 10
``````

The results are `-7`, `4`, and `10`, respectively.

## Round Negative Digits to Tens

So far, we have covered rounding positive and negative numbers, as well as vectors of positive and negative numbers, to a positive `digits` parameter. Next, we will explore how to round positive and negative values to a negative `digits` parameter.

Pass the number you want to round to the `round()` function along with the `digits` parameter, which specifies a negative value. This will round the number to the nearest power of 10.

``````
# Round negative digit to tens
num <- 346
print("Given number:")
print(num)
print("After Rrounding a negative digit to tens:")
print(round(num, digits = -1))

# Output:
# [1] "Given number:"
# [1] 346
# [1] "After Rrounding a negative digit to tens:"
# [1] 350
``````

In this example, the number `346` is rounded to the nearest ten. The tens digit is rounded up from 4 to 5, resulting in `350`.

## Round Negative Digits to Hundreds in R

In this example, to round the negative value to a negative digits parameter, you can pass the number you want to round to the `round()` function along with the `digits` parameter, which specifies a negative value. This will round the number to the nearest power of 10.

``````
# Round negative digits to hundreds
num <- 987
print("Given number:")
print(num)
print("After Rrounding a negative digit to hundred :")
print(round(num, digits = -2))

# Output:
# [1] "Given number:"
# [1] 987
# [1] "After Rrounding a negative digit to hundred :"
# [1] 1000
``````

The number `987` is rounded to the nearest hundred. The hundreds digit is rounded up from 9 to 10, resulting in `1000`.

## Conclusion

In this article, I explained the `round()` function in R, including its syntax, parameters, and usage for rounding numeric values to the nearest integer or a specified number of decimal places. I also discussed the behavior of the `digits` parameter and how it affects rounding when set to positive or negative values.

Happy Learning!!