R ceiling
ceiling returns the smallest integer-valued number greater than or equal to each input. It works element by element on numeric vectors.
A simple example
ceiling
- Start with 2.3
- Do this Round upward to a whole number
- Result 3
cat(ceiling(2.3), "\n", sep = "")
3
Syntax
ceiling(x)
Run this example in R
Save the program as Lesson.R. Use R 4.x.
Rscript --vanilla Lesson.R
Count containers for each order
A batch of items must be divided into pages, containers or work units of fixed size. Account for the final partial group so the last few items are neither lost nor counted as a full batch incorrectly.
orders <- c(0, 12, 13, NA_real_)
cat(paste(ceiling(orders / 12), collapse = " "), "\n", sep = "")
0 1 2 NA
An exact multiple already needs a whole number of containers. Zero needs none and NA propagates as missing. Binary floating-point representation can affect results close to a boundary.
Book enough minibuses for a field trip
A teacher has 73 passengers on the museum trip, including the adults. Each minibus seats 24. She needs enough vehicles for the last passenger as well as the first 72.
passengers <- 73
seats <- 24
cat(ceiling(passengers / seats), "minibuses")
4 minibuses
Three minibuses seat 72 people. Dividing before rounding up reserves the fourth vehicle for the remaining passenger.
Pack badges for two classrooms
A teacher has 31 badges and uses trays that hold eight each. All badges must travel, including those in a partially filled tray.
Three trays carry 24 badges. The remaining seven need the fourth tray.