如何在R中使用floor()和ceiling()函数
在R中使用floor()和ceiling()函数很容易。
在本教程中,我们重点介绍R中这些功能的工作和使用。
众所周知,数学函数是数据分析的关键组成部分。
今天,我们正在讨论数学功能,例如R中的floor和ceiling.
让我们看看这些函数如何工作。
让我们从语法开始
Floor():Floor是R中的数学函数,它返回小于输入值的最高整数值,即输出值将不大于输入值。
floor(x)
其中:
x =输入向量或者值。
Ceiling():Ceiling函数也是R中的另一个数学函数,它将返回最接近输入值但大于输入值的值。
ceiling(x)
其中x =输入向量或者值
注意:floor()和ceiling()值都将舍入给定的输入值。
但是下限功能将四舍五入到最接近的值,该值也应小于输入值。
对于上限功能,它将四舍五入到最接近的值,该值也应大于输入值。
我知道这些定义可能会使您困惑。
不用担心,我们还将通过多个示例进行说明,以便更好地理解。
R中的floor()函数
正如我们上面讨论的floor()函数可以返回给定输入小数的最接近的较小值。
在下面的示例中,看看它是如何工作的。
#returns the nearest lesser value for the input floor(5)
输出= 5
#returns the nearest lesser value for the input floor(5.9)
输出= 5
现在,让我们看看如何计算负值的下限函数。
#returns the nearest lesser value for the input floor(-5.5)
输出= -6
在这种情况下,-6小于-5.5。
因此,返回了floor()函数,如上所示,输出为-6。
如您在上面的示例和输出中所观察到的,floor()函数返回的最接近值也小于给定的输入,即5和5.9,输出返回为5。
因为5是最接近的值,并且也不大于比输入值大。
对于-5.5,最接近的值为-6。
带向量的floor()函数
在本节中,我们将着重于地板函数其中具有值的向量上的应用。
#creates a vector df<-c(1.34,5.67,8.96,4,5.6678,0.00,6.55) #returns the nearest value which will not be greater than the input values floor(df)
输出= 1 5 8 4 5 0 6
现在,让我们看看floor()函数如何处理负值。
#creates a vector having nagetive values df<-c(-3.45,6.7,-0.7,-5.5,4.4,-9.9) #returns the nearest value which will not be greater than the input values floor(df)
输出= -4 6 -1 -6 4 -10
带有表达式的floor()函数
在本节中,我们将将floor函数应用于表达式。
#returns the final value which is nearest to input and also less than that. floor(-3.55 + 2.566 - 8.99 + 3.44 - 5.98)
输出= -13
如上所示,floor函数将求解表达式或者方程式并四舍五入输出值。
带有数据框的floor()函数
在本节中,我们将将floor()函数应用于数据框,并找到这些值的最接近值。
让我们看看它是如何工作的。
#returns the nearest value of the given input floor(airquality$Wind)
输出=
[1] 7 8 12 11 14 14 8 13 20 8 6 9 9 10 13 11 12 18 11 9 9 16 9 12 [25] 16 14 8 12 14 5 7 8 9 16 9 8 14 9 6 13 11 10 9 8 13 11 14 20 [49] 9 11 10 6 1 4 6 8 8 10 11 14 8 4 9 9 10 4 10 5 6 5 7 8 [73] 14 14 14 14 6 10 6 5 11 6 9 11 8 8 8 12 7 7 7 9 6 13 7 6 [97] 7 4 4 10 8 8 11 11 11 9 11 10 6 7 10 10 15 14 12 9 3 8 5 9 [121] 2 6 6 6 5 2 4 7 15 10 10 10 9 14 15 6 10 11 6 13 10 10 8 12 [145] 9 10 10 16 6 13 14 8 11
R中的ceiling()函数
正如我们在本教程的初始阶段所讨论的,ceiling()函数将为给定的十进制输入返回较高的整数。
让我们看看天花板的工作原理。
#returns the nearest value which is greater than the input ceiling(2.456)
输出= 3
#returns the nearest value which is greater than the input ceiling(-5.98)
输出= -5
从上面的输出中可以看到,ceiling()函数正在将最接近的最大值返回给定的输入。
带矢量的ceiling()函数
在本节中,让我们看看如何将上限函数应用于具有多个值的向量。
#creates a vector df<-c(1.34,5.66,7.89,9.54) #returns the nearest value which is greater than the input ceiling(df)
输出= 2 6 8 10
对于向量中的负值。
#creates a vector df<-c(-2.34,5.67,-9.87,-6.77) #returns the nearest value which is greater than the input ceiling(df)
输出= -2 6 -9 -6
带表达式的ceiling()函数
在本节中,我们将把上限函数应用于表达式。
让我们看看它是如何工作的。
#returns the nearest value which is greater than the input ceiling(-3.45 + 5.678 - 7.890 - 4.678)
输出= -10
#returns the nearest value which is greater than the input ceiling(-3.45 + 5.678 - 7.890 - 4.678 + 6.89000 + 2.456 + 5.678)
输出= 5
带数据框的ceiling()函数
好吧,我们已经将上限函数应用于单个值,多个值,向量和表达式。
现在是将上限()函数应用于数据框的时候了。
为此,我们使用空气质量数据集。
这是空气质量数据集的数据框,默认情况下在R studio中可用。
#returns the nearest value which is greater than the input ceiling(airquality$Wind)
输出=
[1] 8 8 13 12 15 15 9 14 21 9 7 10 10 11 14 12 12 19 12 10 10 17 10 12 [25] 17 15 8 12 15 6 8 9 10 17 10 9 15 10 7 14 12 11 10 8 14 12 15 21 [49] 10 12 11 7 2 5 7 8 8 11 12 15 8 5 10 10 11 5 11 6 7 6 8 9 [73] 15 15 15 15 7 11 7 6 12 7 10 12 9 8 9 12 8 8 8 10 7 14 8 7 [97] 8 5 4 11 8 9 12 12 12 10 12 11 7 8 11 11 16 15 13 10 4 8 6 10 [121] 3 7 7 7 6 3 5 8 16 11 11 11 10 15 16 7 11 12 7 14 11 11 8 13 [145] 10 11 11 17 7 14 15 8 12
您可以看到ceiling函数返回给定输入值的最接近的最大值。
它的ceiling()函数在R中的工作方式。