如何在R中使用floor()和ceiling()函数

Using floor() and ceiling() function in R is easy. In this tutorial, we are focusing on the working and uses of these functions in R.

在R中使用floor()ceiling()函数很容易。 在本教程中,我们重点介绍R中这些功能的工作和使用。

Hello people, I hope you are doing good. As we know mathematical functions are the key components in the data analysis. Today we are discussing the mathematical functions such as the floor and ceiling in R. Let’s see how these functions will work.

大家好,希望您一切都好。 众所周知,数学函数是数据分析的关键组成部分。 今天,我们将讨论R中的地板和天花板等数学函数。让我们看看这些函数如何工作。

让我们从语法开始 (Let’s start with the syntax)

Floor(): Floor is a mathematical function in R, which returns the highest integer values which is smaller than the input value i.e. the output value will not be greater than the input value.

Floor(): Floor是R中的一个数学函数,它返回小于输入值的最大整数值,即输出值将不大于输入值。


floor(x)

Where:

哪里:

x = input vector or a value.

x =输入向量或值。

Ceiling(): The Ceiling function is also another mathematical function in R that will return the value which is nearest to the input value but it will be greater than the input value.

Ceiling(): Ceiling函数也是R中的另一个数学函数,它将返回最接近输入值但大于输入值的值。


ceiling(x)

Where x = input vector or a value

其中x =输入向量或值

Note: Both floor() and ceiling() values will round of the given input values. But floor function will round off the nearest values which should also be less than the input value. In the case of the ceiling function, it rounds off the nearest value which should also be greater than the input value.

注意: floor()和ceiling() 值将舍入给定的输入值。 但是下限功能将四舍五入到最接近的值,该值也应小于输入值。 对于上限功能,它将四舍五入到最接近的值,该值也应大于输入值。

I know that these definitions may create confusion in your mind. No worries, we are illustrating this with multiple examples for a better understanding as well.

我知道这些定义可能会使您困惑。 不用担心,我们还将通过多个示例进行说明,以便更好地理解。



R中的floor()函数 (The floor() function in R)

As we discussed above floor() function can return the nearest lesser value of the given input decimal. Let’s see how it works in the below sample.

正如我们上面讨论的,floor()函数可以返回给定输入小数的最接近的较小值。 在下面的示例中,让我们看看它是如何工作的。


#returns the nearest lesser value for the input
floor(5)

Output = 5

输出= 5


#returns the nearest lesser value for the input
floor(5.9)

Output = 5

输出= 5

Now, let’s see how we can compute the floor function for the negative value.

现在,让我们看看如何计算负值的下限函数。


#returns the nearest lesser value for the input
floor(-5.5)

Output = -6

输出= -6

In this case, -6 is less than -5.5. Hence the floor() function returned, the output as -6 as you can see above.

在这种情况下,-6小于-5.5。 因此,返回了floor()函数,如上所示,输出为-6。

As you may observe in the above samples and outputs, the floor() function returns the nearest value which is also less than the given input i.e. 5 and 5.9 the output returned as 5. Because 5 is the nearest value and also it is not greater than input values. For -5.5 the nearest value is -6.

如您在上面的示例和输出中所观察到的,floor()函数返回的最接近值也小于给定的输入,即5和5.9,输出返回为5。因为5是最接近的值,并且也不大于比输入值大。 对于-5.5,最接近的值为-6。



带向量的floor()函数 (The floor() function with a vector )

In this section, we will be focusing on the application of floor function on the vectors having values in it.

在本节中,我们将着重于地板函数在其中具有值的向量上的应用。


#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)

Output = 1 5 8 4 5 0 6

输出= 1 5 8 4 5 0 6

Now, let’s see how the floor() function works for the negative values.

现在,让我们看看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)

Output = -4 6 -1 -6 4 -10

输出= -4 6 -1 -6 4 -10



带有表达式的floor()函数 (The floor() function with an expression)

In this section we are going to apply the floor function to an expression.

在本节中,我们将将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)

Output = -13

输出= -13

As shown above, the floor function will solve the expression or the equation and rounds off the output value.

如上所示,floor函数将求解表达式或方程式并四舍五入输出值。



带有数据框的floor()函数 (The floor() function with a data frame)

In this section, we are going to apply the floor() function to a data frame and find the nearest value of those values as well.

在本节中,我们将将floor()函数应用于数据框,并找到这些值的最接近值。

Let’s see how it works.

让我们看看它是如何工作的。

Airquality Dataset 1

#returns the nearest value of the given input 
floor(airquality$Wind)

Output =

输出=


[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() function in R)

As we discussed in the initial phases of this tutorial, the ceiling() function will return the higher integer for the given decimal input.

正如我们在本教程的初始阶段所讨论的,ceiling()函数将为给定的十进制输入返回较高的整数。

Let’s see how ceiling works.

让我们看看天花板是如何工作的。


#returns the nearest value which is greater than the input 
ceiling(2.456)

Output = 3

输出= 3


#returns the nearest value which is greater than the input 
ceiling(-5.98)

Output = -5

输出= -5

As you can observe in the above outputs, the ceiling() function is returning the nearest highest value to the given input.

从上面的输出中可以看到,ceiling()函数将最接近的最大值返回给定的输入。



带矢量的ceiling()函数 (ceiling() function with a vector)

In this section, let’s see how the ceiling function can be applied to a vector having multiple values.

在本节中,让我们看看如何将上限函数应用于具有多个值的向量。


#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)

Output = 2 6 8 10

输出= 2 6 8 10

For negative values in a vector.

对于向量中的负值


#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)

Output = -2 6 -9 -6

输出= -2 6 -9 -6



带表达式的ceiling()函数 (ceiling() function with an expression)

In this section, we are going to apply the ceiling function to an expression. Let’s see how it works.

在本节中,我们将把上限函数应用于表达式。 让我们看看它是如何工作的。


#returns the nearest value which is greater than the input 
ceiling(-3.45 + 5.678 - 7.890 - 4.678)

Output = -10

输出= -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)

Output = 5

输出= 5



带数据框的ceiling()函数 (ceiling() function with a data frame)

Well, we have applied the ceiling function to single values, multiple values, vectors, and expressions also. It’s time to apply to the ceiling () function to a data frame. Let’s use the airquality data set for this purpose.

好吧,我们已经将上限函数应用于单个值,多个值,向量和表达式。 现在是将上限()函数应用于数据框的时候了。 为此,我们使用空气质量数据集。

This is the data frame of the airquality data set, which is available in R studio by default.

这是空气质量数据集的数据框,默认情况下在R studio中可用。

Airquality Dataset 2

#returns the nearest value which is greater than the input 
ceiling(airquality$Wind)

Output =

输出=


[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

You can see that the ceiling function returned the nearest highest value of the given input values. Its how the ceiling() function works in R.

您可以看到ceiling函数返回给定输入值的最接近的最大值。 它的ceiling()函数在R中的工作方式。



加起来 (Summing up)

R offers the mathematical functions such as the floor() and ceiling() to compute the mathematical operations such as finding the nearest values of the input.

R提供了数学函数(例如floor()和ceiling())来计算数学运算,例如查找输入的最接近值。

In this tutorial, we have discussed the working of floor and ceiling functions. The floor will return the nearest lower value of the given input and on the other side, the ceiling function will return the nearest higher value of the given input as illustrated in the above samples.

在本教程中,我们讨论了地板和天花板功能的工作。 地板将返回给定输入的最接近的较低值,而在另一侧,上限功能将返回给定输入的最接近的较高值,如上述示例所示。

That’s all about the floor() and ceiling() functions in R. Happy flooring and ceiling!!!

这是所有关于地板()和上限()在R. 快乐职能地板和天花板!

More study: Floor in R – R documentation and ceiling in R – stat.ethz

更多研究: R – R文档中的 下限和R – stat.ethz中的上限

翻译自: https://www.journaldev.com/40991/floor-ceiling-function-in-r

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值