R floor Function

The R floor method is one of the R Math functions, which is to return the largest integer value. That is not greater than (less than) or equal to a specific number or an expression. Let us see how to use the floor in R Programming language with an example.

R floor syntax

The syntax of the floor in R Programming language is

floor(numeric_expression);

Numeric_Expression: It can be a numeric value or a valid numerical expression for which you want to find a square root.

  • If the numeric_expression is positive or Negative numeric value, the floor function returns the floor value.
  • If the numeric_expression is positive or Negative Zero, a function returns Zero
  • numeric_expression is NaN (not a number) then floor function will return NaN.
  • If the numeric_expression is positive or negative infinity, then the function returns the same.

The following floor example shows you the same

# floor in R example

# Use floor Function on Positive, and negative zeros
floor(0)
floor(-0)

# Using floor Function on Not a Number
floor(NaN)
floor(-NaN)

# floor Numbers of +ve and -ve Infinity
floor(Inf)
floor(-Inf)

# Use floor Function on String Data
floor("Tutorial Gateway")
R Floor Function 1

R floor Function example 1

In this program, We are going to find the floor values of different data and display the output

# floor in R example

# Use floor Function on Positive  Value
floor(645.956)
floor(25.225)

# Using floor Function on Negative values
floor(-10.285)
floor(-123.987)

# floor Value of an Expression
floor(-10.986 + 120.456 - 200.423 + 151.67)

# floor Function on vectors
number1 <- c(-25.26, 256.94, -136.42, 183.999 , -155.893)
number2 <- c(-2.45, 22.10, 22.95)
floor(number1)
floor(number2)
R Floor Function 2

Floor Function example 2

In this program, We are going to apply the floor function on List data and display the output. For this example, we are using the airquality data set provided by R

# floor in R example

# Data set that We are going to use
airquality

# Applying floor function on Wind Data in Airquality
floor(airquality$Wind)
R Floor Function 3

R floor Function Example 3

The floor function in R programming also allows you to floor the numeric values in a database, or a table column. In this example, We are going to find the floor values of all the records present in [Standard Cost] and [Sales Amount] columns.

For this, we use the below-shown CSV data, and I suggest you refer to the R Read CSV Function article to understand importing the CSV file in R Programming.

R Floor Function 5

From the below code and screenshot, you can observe that R floor function is returning the closest integer value, which is less than or equal to the values in the Standard Cost, and Sales Amount column

# floor in R example

#Current Working DIrectory
getwd()

#Datat We are going to use
product <- read.csv("Product_Data.csv", TRUE, sep = ",")
print(product)

# Applying Floor function on Standard Cost, and Sales Amount Columns
floor(product$StandardCost)
floor(product$SalesAmount)
R Floor Function 4