R 的 floor 方法是 R Math 函数之一,用于返回大于或等于某个特定数字或表达式的最大整数值。下面我们来看一个示例,展示如何在 R 编程语言中使用 floor 函数。
R floor 语法
R 编程语言中 floor 函数的语法是:
floor(numeric_expression);
Numeric_Expression:这可以是一个数值或一个有效的数值表达式,您希望为其找到平方根。
- 如果 numeric_expression 是正数或负数,floor 函数将返回 floor 值。
- 如果 numeric_expression 是正数或负零,函数将返回零。
- 如果 numeric_expression 是 NaN(非数字),则 floor 函数将返回 NaN。
- 如果 numeric_expression 是正无穷或负无穷,则函数返回相同的值。
下面的 floor 示例向您展示了这一点:
# 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 函数示例 1
在此程序中,我们将查找不同数据的 floor 值并显示输出。
# 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)

Floor 函数示例 2
在此程序中,我们将把 floor 函数应用于 List 数据并显示输出。对于此示例,我们使用 R 提供的 airquality 数据集。
# 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 函数示例 3
R 编程中的 floor 函数还允许您对数据库或表列中的数值进行 floor 操作。在此示例中,我们将查找 [Standard Cost] 和 [Sales Amount] 列中所有记录的 floor 值。
为此,我们使用下面显示的 CSV 数据,并建议您参考 R Read CSV Function 文章,以了解如何在 R 编程中导入 CSV 文件。
从下面的代码和屏幕截图中,您可以看到 R floor 函数返回了小于或等于 Standard Cost 和 Sales Amount 列中值的最接近的整数值。
# 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)
