Skip to Content

How to use the ceiling() function in R?

In this tutorial, we will look at how to use the built-in ceiling() function in R with the help of some examples.

What does the ceiling() function do in R?

You can use the built-in math function, ceiling() to get the smallest integer greater than or equal to a given number in R. Pass the number for which you want to get the ceiling as an argument to the ceiling() function. The following is the syntax –

📚 Discover Online Data Science Courses & Programs (Enroll for Free)

Introductory ⭐

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

ceiling(x)

If you pass an integer to the ceiling() function, you’ll get the same value as the output. Note that you can apply the ceiling() function to a numeric vector, array, matrix, and a dataframe as well.

Examples

Let’s look at some examples of using the ceiling() function in R.

Apply ceiling() function to a number

First, let’s look at some examples of using the ceiling() function on a positive real number.


Upskill your career right now →

# ceiling for a positive real number
print(ceiling(3.2))
print(ceiling(3.4))
print(ceiling(3.7))

Output:

[1] 4
[1] 4
[1] 4

You can see that we get the same output, 4 for the values, 3.2, 3.4, and 3.7. Notice that 4 is the smallest integer that is greater than or equal to the above values and thus we get 4 as the output for all the values in the above example.

Let’s now apply the ceiling function to negative real numbers.

# ceiling for a negative real number
print(ceiling(-3.2))
print(ceiling(-3.4))
print(ceiling(-3.7))

Output:

[1] -3
[1] -3
[1] -3

We get -3 as the result for the numbers, -3.2, -3.4, and -3.7. Here, -3 is the smallest integer that is greater than or equal to all the passed values.

Apply ceiling() function to a numeric vector

You can similarly apply the ceiling() function to a numeric vector in R. If you apply the ceiling() function to a numeric vector, it will compute the ceiling for each value in the vector.

Let’s look at an example.


Upskill your career right now →

# ceiling for a numeric vector
vec <- c(0, 1.3, -1.3, 2.5, 3.1)
print(ceiling(vec))

Output:

[1]  0  2 -1  3  4

You can see that we get the ceil (or the ceiling value) for each value in the numeric vector.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Authors

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

  • Gottumukkala Sravan Kumar