R语言 的四舍五入–round()函数

R语言 的四舍五入–round()函数

R语言中的 round() 函数用于将数值四舍五入到一个特定的小数点。

语法: round(x, digits)

参数:

x: 要舍弃的值

digits: 要舍弃的值的位数

例1:数值进行四舍五入

# R program to illustrate
# round function
 
# Create example values
x1 <- 1.2                          
x2 <- 1.8
x3 <- - 1.3
x4 <- 1.7
 
# Apply round function
round(x1)                          
round(x2)
round(x3)
round(x4)
 
print(x1, x2, x3, x4)

输出 :

 1
 2
-1
 2

在上面的代码中,我们用函数 round() 将4个数据x1, x2, x3, x4四舍五入,并打印出新的数值。

例2:四舍五入到某些数字

# R program to illustrate
# round to a certain digit
 
# Create numeric with many digits
x2 <- 3.14734343243  
 
# Round to three decimal places                 
round(x2, digits = 3) 
 
print(x2)            

输出

 3.147 

在上述代码中,数字向量的值被四舍五入到3位。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程