floor in R: How to Calculate Floor of Number

R floor() method returns the largest integer that is smaller than or equal to the value passed to it as an argument. The ceiling() in the R method returns the smallest integer greater than or equal to the value passed to it as a parameter.

floor() in R

The floor() is a built-in R function that takes the vector or column of the data frame and rounds down those values. The floor() method takes an input, rounds to the nearest integer smaller than the input value, and returns it.

Syntax

floor(numeric_value)

Parameters

The floor() method takes a numeric_value as an argument.

  1. If the numeric_value is a positive or negative numeric value, the floor() function returns the floor value.
  2. If the numeric_value is positive or Negative Zero, the function returns Zero.
  3. If the numeric_value is NaN (not a number), then the floor function will return NaN.
  4. If the numeric_value is positive or negative infinity, then the function returns the same.

Example

floor(2.1)
floor(1.9)

Output

[1] 2
[1] 1

Even if the value 1.9 is near 2, it still returns the lower rounding value, which is 1 in our case; that is why it returns 1.

Find the Floor value of negative value in R

To get the floor value of negative value in R, use the floor() function. The floor() method takes a negative value as an input and returns its floor value.

floor(-2.1)
floor(-1.9)

Output

[1] -3
[1] -2

Both floor() and ceiling() values will round the given input values. But the floor function will round off the lowest nearest values, which should also be less than the input value.

Calculating floor value of a Vector

The floor() method takes up the Vector as an argument and rounds down all the vector values without decimal places so as no decimal values are left.

rv <- c(1.11, 3.9, 2.1, 1.9, 1.8, 2.9)
floor(rv)

Output

[1] 1 3 2 1 1 2

R floor() function with an expression

We can apply the floor() function to an expression.

floor(2.1 + 1.9 - 4.6 + 1.8 + 2.9)

Output

[1] 4

Applying floor() function to a data frame

To calculate the floor of a column in a data frame, use the floor() method. We use the built-in dataset mtcars and apply the floor() function to the wt column.

data(mtcars)
head(mtcars)
cat("\n")
cat("Floor values of wt column", "\n")
cat("\n")
floor(mtcars$wt)

Output


                   mpg   cyl  disp  hp  drat  wt   qsec  vs  am gear carb
Mazda RX4          21.0   6   160  110  3.90 2.620 16.46  0  1   4    4
Mazda RX4 Wag      21.0   6   160  110  3.90 2.875 17.02  0  1   4    4
Datsun 710         22.8   4   108  93   3.85 2.320 18.61  1  1   4    1
Hornet 4 Drive     21.4   6   258  110  3.08 3.215 19.44  1  0   3    1
Hornet Sportabout  18.7   8   360  175  3.15 3.440 17.02  0  0   3    2
Valiant            18.1   6   225  105  2.76 3.460 20.22  1  0   3    1

Floor values of wt column

[1] 2 2 2 3 3 3 3 3 3 3 3 4 3 3 5 5 5 2 1 1 2 3 3 3 3 1 2 1 3 2 3 2

You can see from the output that the floor() function returns the nearest values of the input.

Conclusion

R language provides the mathematical functions such as the floor() and ceiling() to calculate the mathematical operations such as finding the nearest values of the input.

That is it for floor() function in R.

See also

R absolute value

R var()

R quantile()

Leave a Comment