四舍五入函数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!!!
好吧,在本教程中,我们将在各个方面对数字进行四舍五入。 来吧!!!
The syntax of the round() function is given below,
round()函数的语法如下所示:
round(x,digits=0)
where,
哪里,
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()函数,您也可以轻松地将数字与特定的小数点舍入。
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
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
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,如上所示。 另外,请注意,不管指定的整数值的类型如何,过程如何继续保持相同。
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
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(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
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
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