The R floor method is one of the R Math function, which is used to return the largest integer value which is not greater than (less than) or equal to an individual number, or an expression. In this article, we will show you, How to use the floor in R Programming language with an example.
R floor syntax
The basic syntax of the floor in R Programming language is as shown below:
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, floor function will return the floor value.
- If the numeric_expression is positive or Negative Zero, a function will return 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 will return the same
Following example will show 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 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 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 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 will use the below-shown csv data, and I suggest you refer to R Read CSV Function article to understand the steps involved in importing CSV file.
# 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)
From the above screenshot, you can observe that 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
Thank You for Visiting Our Blog