Library contents

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

  1. Start with 2.3
  2. Do this Round upward to a whole number
  3. Result 3
Two is below 2.3. Three is the first whole number at or above it.
Lesson.R
cat(ceiling(2.3), "\n", sep = "")
Output
3

Syntax

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

From the programcat(paste(ceiling(orders / 12), collapse = " "), "\n", sep = "")
Result0 1 2 NA
Lesson.R
orders <- c(0, 12, 13, NA_real_)
cat(paste(ceiling(orders / 12), collapse = " "), "\n", sep = "")
Output
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.

R documentation for ceiling

Advertisements

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.

Given73 passengers · 24 seats per minibus
Result4 minibuses
Lesson.R
passengers <- 73
seats <- 24
cat(ceiling(passengers / seats), "minibuses")
Output
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.

Given31 badges · 8 per tray
Worked result4 trays

Three trays carry 24 badges. The remaining seven need the fourth tray.