Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将数字四舍五入到最接近的零_R - Fatal编程技术网

R 将数字四舍五入到最接近的零

R 将数字四舍五入到最接近的零,r,R,我有两种数字模式: x1 = 0.0003577 x2 = 0.2365483 我怎样才能使他们变得像: x1 = 0.00035 x2 = 0.23 因此,逗号后最后一个零后只出现两个或一个数字?您可以在base包中使用signif。函数将其第一个参数中的值舍入到指定的有效位数 x1 = 0.0003577 x2 = 0.2365483 #where 2 is the number of significant digit signif(x1,2) [1] 0.00036 signif(

我有两种数字模式:

x1 = 0.0003577
x2 = 0.2365483
我怎样才能使他们变得像:

x1 = 0.00035
x2 = 0.23

因此,逗号后最后一个零后只出现两个或一个数字?

您可以在
base
包中使用
signif
。函数将其第一个参数中的值舍入到指定的有效位数

x1 = 0.0003577
x2 = 0.2365483

#where 2 is the number of significant digit
signif(x1,2)
[1] 0.00036
signif(x2,2)  
[1] 0.24

您可以在
base
软件包中使用
signif
。函数将其第一个参数中的值舍入到指定的有效位数

x1 = 0.0003577
x2 = 0.2365483

#where 2 is the number of significant digit
signif(x1,2)
[1] 0.00036
signif(x2,2)  
[1] 0.24

您可以像下面那样定义用户函数
f

f <- function(x) {
  p <- 10^(-ceiling(log10(x)) + 2)
  trunc(x * p) / p
}

您可以像下面那样定义用户函数
f

f <- function(x) {
  p <- 10^(-ceiling(log10(x)) + 2)
  trunc(x * p) / p
}

谢谢但我想得到完全相同的数字。e、 g.x1=0.00035。这是最好的方法。0.00036已正确四舍五入。0.00035只是被丢弃的数字截断,但这样做会引入偏差。谢谢。但我想得到完全相同的数字。e、 g.x1=0.00035。这是最好的方法。0.00036已正确四舍五入。0.00035只是被丢弃的数字截断,但这样做会引入偏差。