R floor
floor returns the largest integer-valued number less than or equal to each input. It can assign observations to half-open intervals of width one.
A simple example
floor
- Start with 2.8
- Do this Round downward to a whole number
- Result 2
cat(floor(2.8), "\n", sep = "")
2
Syntax
floor(x)
Run this example in R
Save the program as Lesson.R. Use R 4.x.
Rscript --vanilla Lesson.R
Place signed readings into unit intervals
A measurement below zero falls between two numbered intervals. Work through how negative values are assigned so the interval on the lower side of zero is labeled consistently with the positive side.
readings <- c(-0.2, 0, 1.0)
cat(paste(floor(readings), collapse = " "), "\n", sep = "")
cat(paste(trunc(readings), collapse = " "), "\n", sep = "")
-1 0 1 0 0 1
The reading −0.2 belongs to the interval starting at −1. Truncation gives zero instead. Exact endpoints stay at their own interval boundary.
Cut complete straps from a roll
The costume room has 8.5 metres of ribbon left before a performance. Each bag needs one unbroken 1.2-metre strap. The volunteer needs the number of bags she can finish from this roll.
metres <- 8.5
per_strap <- 1.2
cat(floor(metres / per_strap), "straps")
7 straps
Seven straps use 8.4 metres. The last tenth of a metre cannot make another strap.
Make complete paint sets
A studio has 29 paint tubes and puts six in each set. It lists only sets that can be handed out complete.
Four sets use 24 tubes. Five tubes stay on the shelf.