四舍五入函数round_如何在R中使用round()将数字四舍五入

四舍五入函数round

Rounding off the numbers can be achieved with round() in R. Round is one of the best ways to make numerical values more meaningful and to get more precise results in the analysis.

可以使用R中的round()将数字四舍五入。Round是使数值更有意义并在分析中获得更精确结果的最佳方法之一。

R offers the standard function round() to round off the numbers based on decimal values as well.

R提供了标准函数round(),也可以根据十进制值对数字进行四舍五入。

Well, in this tutorial we are going to round off the numbers in various aspects. Let’s roll!!!

好吧,在本教程中,我们将在各个方面对数字进行四舍五入。 来吧!!!



从R中的round()语法开始 (Starting with the syntax of round() in R)

The syntax of the round() function is given below,

round()函数的语法如下所示:


round(x,digits=0)

where,

哪里,

  • x -> numerical value or a vector available for round off

    x->数值或可用于四舍五入的向量
  • digits -> This value represents the number of decimal points that you wish to have for the number

    digits->此值表示您希望数字的小数位数


在R中使用round()舍入数字 (Rounding off the numbers using round() in R)

Some of you may know that R can accurately calculate up to 16 digits. But you always don’t need those 16 digits for your analysis. So using the round() function in R, you can easily round the numbers with specific decimal points as well.

你们中的某些人可能知道R可以准确计算最多16位数字。 但是,您始终不需要这16位数字进行分析。 因此,使用R中的round()函数,您也可以轻松地将数字与特定的小数点舍入。



四舍五入单个数值 (Round off a single numerical value )

Here we are going to round off a single value using the function round().

在这里,我们将使用功能round()舍入单个值。

As the below output shows, the digit value will indicate the decimal points.

如以下输出所示,数字值将指示小数点。


round(143.1234, digits = 0)

Output: 143

输出: 143


round(143.1234, digits = 1)

Output: 143.1

输出: 143.1


round(143.1234, digits = 2)

Output: 143.12

输出: 143.12



向量中的值取整 (Round Up Values in a Vector)

Now let’s round off the values stored in a vector.

现在,让我们四舍五入存储在vector中的值。


#creates a vector with multiple numerical values in it
df<-c(143.236,34.765,789.654,224.568,23.567,9.76)

#round off the number in a vector with decimal point 2
round(df, digits = 2)

Output= 143.24 34.77 789.65 224.57 23.57 9.76

输出= 143.24 34.77 789.65 224.57 23.57 9.76


df<-c(143.236,34.765,789.654,224.568,23.567,9.76)

#round off the number in a vector with decimal point 3
round(df, digits = 3)

Output= 143.236 34.765 789.654 224.568 23.567 9.760

输出= 143.236 34.765 789.654 224.568 23.567 9.760


df<-c(143.236,34.765,789.654,224.568,23.567,9.76)

#round off the number in a vector with decimal point 0
round(df, digits = 0)

Output= 143 35 790 225 24 10

输出= 143 35 790 225 24 10



在R中舍入负数 (Rounding Negative Numbers in R)

Sometimes the data may also consist of negative values. Let’s see how we can round off the negative values in a vector.

有时数据也可能包含负值。 让我们看看如何舍入向量中的负值。


df<-c(-143.236,-34.765,-789.654,-224.568,-23.567,-9.76)
round(df, 2)

Output= -143.24 -34.77 -789.65 -224.57 -23.57 -9.76

输出= -143.24 -34.77 -789.65 -224.57 -23.57 -9.76


df<-c(-143.236,-34.765,-789.654,-224.568,-23.567,-9.76)
round(df)

Output = -143 -35 -790 -225 -24 -10

输出= -143 -35 -790 -225 -24 -10

Note: Keep in mind that if you did not mention the digits, by default it will be considered as 0 as shown above. Also, notice how the process continues to be the same irrespective of the type of integer value specified.

注意:请记住,如果您未提及数字,则默认情况下,它将被视为0,如上所示。 另外,请注意,不管指定的整数值的类型如何,过程如何继续保持相同。



四舍五入表达式中的值 (Round off the values in an expression )

Using round() in R we can easily round off the numbers in an expression.

在R中使用round()可以轻松舍入表达式中的数字。

The round() function will solve the expression and rounds off the final value with 2 decimal points as shown below.

round()函数将求解表达式,并用2个小数点舍入最终值,如下所示。


#creats an expression
my_expression<- c(34.567+234.6789-234.67+567.8)

#round off the values in an expression 
round(my_expression)

Output= 602.38

输出= 602.38



在R中使用round()对数据集中的值进行舍入 (Round values in a dataset using round() in R)

We have gone through various aspects of a round() function. Let’s round off the values present in an iris dataset.

我们已经研究了round()函数的各个方面。 让我们四舍五入虹膜数据集中的值


df<- datasets::iris
df

Iris dataset view

虹膜数据集视图

Round In R 3

round(iris$Sepal.Length,0)

Output= All the values are rounded with 0 decimal points.

输出=所有值均以0小数点舍入。


[1] 5 5 5 5 5 5 5 5 4 5 5 5 5 4 6 6 5 5 6 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 5 5
[37] 6 5 4 5 5 4 4 5 5 5 5 5 5 5 7 6 7 6 6 6 6 5 7 5 5 6 6 6 6 7 6 6 6 6 6 6
[73] 6 6 6 7 7 7 6 6 6 6 6 6 5 6 7 6 6 6 6 6 6 5 6 6 6 6 5 6 6 6 7 6 6 8 5 7
[109] 7 7 6 6 7 6 6 6 6 8 8 6 7 6 8 6 7 7 6 6 6 7 7 8 6 6 6 8 6 6 6 7 7 7 6 7
[145] 7 7 6 6 6 6

Signif()函数将数字取整 (Signif() function to round off the numbers)

If you wonder, what is signif()? It is like a round function, but it looks for total digits instead of decimal points alone.

如果您想知道,什么是signif()? 它就像一个舍入函数,但是它只寻找总位数而不是小数点。


signif(34.567, 3)

Output= 34.6

输出= 34.6

Here you can observe that the signif() function will round off the total number of digits. It doesn’t deal with the decimal points.

在这里,您可以观察到signif()函数将舍入位数的总数。 它不处理小数点。


signif(34.567, 5)

Output= 34.567

输出= 34.567





结语 (Wrapping up)

R is very aggressive in data analysis. Round off the values found greater importance in data analysis as it yields good accuracy in the results. With the help of round() function in R, you can easily round off the values with specific decimal points as well.

R在数据分析方面非常积极。 四舍五入后的值在数据分析中显得更为重要,因为它可以使结果具有良好的准确性。 借助R中的round()函数,您还可以轻松地将值与特定的小数点四舍五入。

Try rounding off more values and stay connected for more R tutorials. Happy roundoff!!!

尝试四舍五入更多值,并保持联系以获取更多R教程。 快乐的舍入

For more study: R documentation/round

欲了解更多研究: R文档/回合

翻译自: https://www.journaldev.com/40146/round-in-r

四舍五入函数round

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值