Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.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_Function_Rounding - Fatal编程技术网

R 自定义整数函数

R 自定义整数函数,r,function,rounding,R,Function,Rounding,我正在尝试创建一个函数(my.func),该函数可以有条件地将数字/数值向量/矩阵/数据.frame四舍五入到其值 例如:my.func(1630.123)将给出1630,而my.func(0.123)将返回0.12 我试过: my.func <- function(x0, ...) { unlist(lapply(x0, function(x) { if(x >= 100) return(round(x, 0)) if(x >= 10 & x &l

我正在尝试创建一个函数(
my.func
),该函数可以有条件地将数字/数值向量/矩阵/数据.frame四舍五入到其值

例如:
my.func(1630.123)
将给出1630,而
my.func(0.123)
将返回0.12

我试过:

my.func <- function(x0, ...) {
  unlist(lapply(x0, function(x) {
    if(x >= 100) return(round(x, 0))
    if(x >= 10 & x < 100) return(round(x, 1))
    if(x >= 0.1 & x < 10) return(round(x, 2))
    if(x >= 0.01 & x < 0.1) return(round(x, 3))
    if(x >= 0.001 & x < 0.01) return(round(x, 4))
    if(x >= 0.0001 & x < 0.001) return(round(x, 5))
    if(x >= 0.00001 & x < 0.0001) return(round(x, 6))
    if(x >= 0.000001 & x < 0.00001) return(round(x, 7))
    if(x >= 0.0000001 & x < 0.000001) return(round(x, 8))
    if(x < 0.0000001) return(x)
  }))
}
my.func=100)返回(四舍五入(x,0))
如果(x>=10&x<100)返回(第(x,1)轮)
如果(x>=0.1&x<10)返回(第(x,2)轮)
如果(x>=0.01&x<0.1)返回(第(x,3)轮)
如果(x>=0.001&x<0.01)返回(第(x,4)轮)
如果(x>=0.0001&x<0.001)返回(第(x,5)轮)
如果(x>=0.00001&x<0.0001)返回(第(x,6)轮)
如果(x>=0.000001&x<0.00001)返回(第(x,7)轮)
如果(x>=0.0000001&x<0.000001)返回(第(x,8)轮)
如果(x<0.0000001)返回(x)
}))
}
适用于数字和向量,但不适用于矩阵/data.frame

我可以将
if
和循环放入矩阵/df,但可能还有另一种方法,更快更简单

另一个例子:


my.func(c(1.314315125135.1234,0.0124133,00000234))
期望返回1.31125135,0.012,0.0000023

您可能需要将
signif
四舍五入到给定的有效位数

x <- runif(16) * 10^(7*runif(16)-5)
cbind(x, mx = my.func(x), sx = signif(x, 3))
还是有什么特殊的原因需要0以上的额外数字?也就是说,你为什么不想

if(x >= 10 & x < 100) return(round(x, 1))
if(x >= 1 & x < 10) return(round(x, 2))
if(x >= 0.1 & x < 1) return(round(x, ?))   # <----- This line
if(x >= 0.01 & x < 0.1) return(round(x, 3))
if(x >= 0.001 & x < 0.01) return(round(x, 4))
另一个函数可能对
formatC
感兴趣,但是您必须接受非常大或非常小的值的科学符号

formatC(x, digits=3)

它不适用于data.frame,因为R不能用不同的数字对不同的行进行取整。因此,必须在变量中选择要应用函数的数字,例如最大值,如下所示:

myfun = function(val) {

  cutvec = 10^(-7:2)
  cutvec = cutvec[!cutvec == 1]
  rounding = 8:0

  find.round = rounding[max(which(val > cutvec))]

  return(find.round)
}

data(mtcars)
digits = myfun(max(mtcars$disp))
round(mtcars$disp, digits)

很好的功能
signif
。非常接近我的结果。实际上,此功能的目标只是可视化。我的闪亮应用程序上有一个表格,上面显示了模型的系数。COEF值非常不同,从1e-08到1000+。因此,我试图找到一个函数,它可以将数字转换为更“好”的值,以便于快速更新。但是这个解决方案怎么样?感谢更新中的解决方案,第一个解决方案正是a想要的!很抱歉回答得太晚,需要做很多工作…据我所知,它对所有vector
mtcars$disp
应用相同的位数进行舍入。这是真的吗?是的,如果你在一个数据框中,R不能在不同的行中应用不同的数字(x,数字)。我认为R能够做到,问题是找到正确的方法。我还在不同的来源中寻找解决方案
 [1] "0.000226" "3.8"      "363"      "1380"     "0.0016"   "0.000331"
 [7] "0.000047" "0.20"     "0.047"    "105"      "22"       "0.000013"
[13] "0.000013" "0.054"    "2.6"      "0.31"
formatC(x, digits=3)
 [1] "0.000226" "3.82"     " 363"     "1.38e+03" "0.00163"  "0.000331"
 [7] "4.68e-05" "0.208"    "0.0479"   " 105"     "22.6"     "1.25e-05"
[13] "1.25e-05" "0.0542"   "2.69"     "0.311"
myfun = function(val) {

  cutvec = 10^(-7:2)
  cutvec = cutvec[!cutvec == 1]
  rounding = 8:0

  find.round = rounding[max(which(val > cutvec))]

  return(find.round)
}

data(mtcars)
digits = myfun(max(mtcars$disp))
round(mtcars$disp, digits)