Library contents

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

  1. Start with 2.8
  2. Do this Round downward to a whole number
  3. Result 2
Two is the greatest integer at or below 2.8.
Lesson.R
cat(floor(2.8), "\n", sep = "")
Output
2

Syntax

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

From the programcat(paste(floor(readings), collapse = " "), "\n", sep = "")
First output line-1 0 1
Lesson.R
readings <- c(-0.2, 0, 1.0)
cat(paste(floor(readings), collapse = " "), "\n", sep = "")
cat(paste(trunc(readings), collapse = " "), "\n", sep = "")
Output
-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.

R documentation for floor

Advertisements

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.

Given8.5 m of ribbon · 1.2 m per strap
Result7 straps
Lesson.R
metres <- 8.5
per_strap <- 1.2
cat(floor(metres / per_strap), "straps")
Output
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.

Given29 tubes · 6 per set
Worked result4 sets

Four sets use 24 tubes. Five tubes stay on the shelf.