Built with Tutify.io

    AIStudyLabs

    Explain round() Function in R

    Explain round() Function in R

    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-

    <a id="h-r-round-function"></a>

    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).

    <a id="h-the-syntax-of-the-round-function"></a>

    The syntax of the round() function

    Following is the syntax of the round() function.

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

    <a id="h-parameters"></a>

    Parameters

    This function allows two parameters.

    <a id="h-return-value"></a>

    Return Value.

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

    <a id="h-round-a-positive-number-to-the-nearest-integer"></a>

    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.

    <a id="h-round-a-negative-number-to-the-nearest-integer"></a>

    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.

    <a id="h-round-a-number-to-a-specified-decimal-place"></a>

    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.

    <a id="h-round-a-negative-number-to-a-specified-decimal-place"></a>

    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.

    <a id="h-round-a-vector-of-positive-numbers-to-nearest-integer"></a>

    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.

    <a id="h-round-a-vector-of-negative-numbers-to-the-nearest-integer"></a>

    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.

    <a id="h-round-a-r-vector-of-mixed-numbers-to-the-nearest-integer"></a>

    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.

    <a id="h-round-negative-digits-to-tens"></a>

    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.

    <a id="h-round-negative-digits-to-hundreds-in-r"></a>

    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.

    <a id="h-conclusion"></a>

    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!!

    <a id="h-related-articles"></a>

    Related Articles