ceiling Function in R: How to Calculate Ceiling of Number

The ceil() function is an alias for the ceiling() method and rounds to the smallest integer equal to or above the provided number.

ceiling Function in R

The ceiling() is an a built-in mathematical R function that returns the smallest integer that is greater than or equal to the value passed to it as an argument. The ceiling() function rounds up to the nearest integer.

Syntax

 ceiling(x)

Parameters

The ceiling() function accepts one parameter whose value needs to be rounded off.

  1. If the x parameter is a positive or negative numeric value, the ceiling function returns the ceiling value.
  2. If it is a positive or negative Zero value, the ceiling(,) function returns Zero.
  3. If the x is Not a Number (NaN), then the ceiling returns NaN.
  4. If the x is positive or negative infinity, the function returns the same as the input.

Example

dice <- ceiling(1.9)
ace <- ceiling(2.1)
club <- ceiling(4.5)

print(dice)
print(ace)
print(club)

Output

[1] 2
[1] 3
[1] 5

From the output, you can see that ceiling() function rounding up 1.9 to 22.1 to 3, and 4.5 to 5.

Passing negative values to ceiling() function

Let’s pass the negative arguments to the ceiling() function in R.

dice <- ceiling(-1.9)
ace <- ceiling(-2.1)
club <- ceiling(-4.5)

print(dice)
print(ace)
print(club)

Output

[1] -1
[1] -2
[1] -4

Ceiling value of an expression

Let’s pass the numeric expression to the ceil() function and see the output.

joker <- ceiling(0.8 + 12.45 - 20.3 + 333)
print(joker)

Output

[1] 326

Applying ceiling() function on R Vectors

To find the ceil values of vectors in R, use the ceiling() function. The ceiling() function returns the ceiling value of each element of the vector.

hearts <- c(-21.19, 46.21, -11.10, -9.11, -0.123)
ceiling(hearts)

Output

[1] -21 47 -11 -9 0

Applying ceiling() function on R List

To find the ceiling value of each element of R List, access elements one by one and pass that element to the ceiling() function, and it will return the ceiling value of the R List.

diamond <- list(10.21, 11.21, 19.21, 29.10)
ceiling(diamond[[1]])
ceiling(diamond[[2]])
ceiling(diamond[[3]])
ceiling(diamond[[4]])

Output

[1] 11
[1] 12
[1] 20
[1] 30

Conclusion

R ceiling() function returns the smallest integer value, which is greater than or equal to a specific number.

That is it for this tutorial.

See also

R absolute value

R round

R Square root

Leave a Comment