四舍五入函数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 offx->数值或可用于四舍五入的向量
  • digits -> This value represents the number of decimal points that you wish to have for the numberdigits->此值表示您希望数字的小数位数


在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(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

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

  1. js保留两位小数的函数_如何在Excel中使用ROUND系列函数

    1. ROUND函数是对某个数字,根据指定的小数位进行四舍五入. 2. 先来看一个简单的例子"=ROUND(3.1415,2)",这是取3.1415的小数点后两位,四舍五入,得到3 ...

  2. 如何在R中正确使用列表?

    本文翻译自:How to Correctly Use Lists in R? Brief background: Many (most?) contemporary programming langu ...

  3. rstudio r语言_如何在R中接受用户输入?

    rstudio r语言 Taking a user input is very simple in R using readline() function. In this tutorial, we ...

  4. 如何在 R 中计算调整后的 R 平方

    如果有什么问题和项目作业关于R语言,可以微信call我:RunsenLiu R 平方,通常写成 R 2,是响应变量中的方差比例,它可以由线性回归模型中的预测变量来解释. R-squared 的值可以在 ...

  5. 如何在 R 中找到 F 临界值

    当您进行 F 检验时,您将获得 F 统计量作为结果.要确定 F 检验的结果是否具有统计显着性,可以将 F 统计量与 F 临界值进行比较.如果 F 统计量大于 F 临界值,则检验结果具有统计显着性. F ...

  6. 如何在 R 中计算 Cramer V

    Cramer's V是衡量两个名义变量之间关联强度的指标. 它的范围从 0 到 1,其中: 0表示两个变量之间没有关联. 1表示两个变量之间存在强关联 本教程提供了几个示例,说明如何在 R 中计算列联 ...

  7. 如何在 R 中执行稳健回归

    当我们正在使用的数据集中存在异常值或有影响的观察值时,稳健回归是我们可以用作普通最小二乘回归的替代方法. 为了在 R 中执行稳健的回归,我们可以使用 MASS包中的rlm()函数 ,它使用以下语法: ...

  8. rstudio中位数的公式_如何在R中找到中位数

    rstudio中位数的公式 In this tutorial, let's learn how we can find the median in R. Median is defined as th ...

  9. csv文件示例_如何在R中使用数据框和CSV文件-带有示例的详细介绍

    csv文件示例 Welcome! If you want to start diving into data science and statistics, then data frames, CSV ...

最新文章

  1. 简单计算器(信息学奥赛一本通-T1057)
  2. 01 能够使用Format实现格式化输出显示
  3. C++中Future和Promise的一种简单实现
  4. 直指Adobe的龌龊行径
  5. pytorch learning
  6. 【小程序】使用socket实现文件的收发
  7. 国产操作系统(Linux)技术流派
  8. JP-Word 简谱编辑(JPW简谱)打谱软件免费版下载 WiN
  9. windows 使用docker构建镜像
  10. 在线订餐系统php心得体会_php网上订餐管理系统
  11. 印度公司注册数据[1857-2020]
  12. monodepth2训练细节
  13. Win10系统重装方法 简单实现一键重装win10系统
  14. 双网络安全nvr/布控球,可双向同时接入国网B接口视频监控平台和国标28181平台
  15. 长隆大马戏机器人_长隆娱乐登陆
  16. Java顺序表 实现扑克牌游戏简单 (梭哈 / 斗牛)
  17. 2023广州大米展会
  18. php代码生成折现统计图
  19. P2P流媒体开源项目介绍
  20. c语言路径搜索,c语言程序(单元路劲及多元路径的搜索)

热门文章

  1. window10家庭版安裝docker遇到的問題【已解決】
  2. UE5 Gameplay 框架拆解
  3. [Python]将MP3和PDF按名字分类归档到各自文件夹
  4. 一个简单光栅器的实现(五) 光栅化阶段
  5. html整体图片拆分拼图代码源代码,canvas拼图功能实现代码示例
  6. 电商平台后台管理系统--->系统详细设计(订单管理模块)
  7. AlphaGo之父戴密斯·哈萨比斯:是天才,也是生活里的普通人
  8. 《指数基金投资指南》读书笔记_2022002
  9. 说明书丨Abnova EDA(人)重组蛋白
  10. Python字符串前缀u、r、b、f含义