Display the output rounded with 3 digits in R.

huangapple go评论66阅读模式
英文:

Force R to display the output rounded with 3 digits

问题

I using R with Rstudio. I want to change the default behavior of R, preferably via options(), to show the numeric results of the output (i.e., the values printed in the console) rounded with 3 decimal digits.

I know that I can use round( x, digits = 3) command, but my objective is to compare "by eye" several variables, and enclosing all variables with the "round" (or "format") command is cumbersome.

An example of what I want:
I want to the command matrix(c(0.00003,0.1111111),ncol=1) give the output:

        [,1] 
   [1,] 0.000
   [2,] 0.111

I try to use options(digits=3), but with that command, some results are displayed in scientific notation. I also try options(digits=3, scipen = 999) but the scipen argument makes the R to print more decimal places in the output.

Ps.: I want to change only the output, not the variable values.

英文:

I using R with Rstudio. I want to change the default behavior of R,preferably via options(), to show the numeric results of the output (ie, the values printed in the console) rounded with 3 decimal digits.

I know that I can use
round( x, digits = 3) command, but my objective is to compare "by eye" several variables, and enclosing all variables with the "round" (or "format") command is cumbersome.

An example of what I want:
I want to the command matrix(c(0.00003,0.1111111),ncol=1) give the output:

        [,1] 
   [1,] 0.000
   [2,] 0.111

I try to use options(digits=3), but with that command, some results are displayed in scientific notation. I also try options(digits=3, scipen = 999) but the scipen argument makes the R to print more decimal places in the output.

Ps.: I want to change only the output, not the variable values.

答案1

得分: 0

你可以考虑以下方法来定义一个具有不同打印方法的新类:

library(stringr)
options(myMatDigit = 3)

print.myMat <- function(mat)
{
  myMatDigits <- getOption("myMatDigit")
  nb_Col <- ncol(mat)
  nb_Row <- nrow(mat)

  for(i in 1 : nb_Col)
  {
    mat_Char[, i] <- round(mat[, i], digits = myMatDigits)
  }
  
  print(mat_Char)
}

mat <- matrix(c(0.00003, 0.1111111), ncol = 1)
mat <- structure(mat, class = "myMat")

mat

     [,1]
[1,] 0.000
[2,] 0.111

使用这种方法,你可以设置一个全局选项 myMatDigit,然后在类中后续使用它。

英文:

You can consider the following approach to define a new class with a different print method :

library(stringr)
options(myMatDigit = 3)

print.myMat &lt;- function(mat)
{
  myMatDigits &lt;- getOption(&quot;myMatDigit&quot;)
  nb_Col &lt;- ncol(mat)
  nb_Row &lt;- nrow(mat)

  for(i in 1 : nb_Col)
  {
    mat_Char[, i] &lt;- round(mat[, i], digits = myMatDigits)
  }
  
  print(mat_Char)
}

mat &lt;- matrix(c(0.00003, 0.1111111), ncol = 1)
mat &lt;- structure(mat, class = &quot;myMat&quot;)

mat

     [,1]
[1,] 0.000
[2,] 0.111

With this approach, you can set a global option with myMatDigit that you can use in the class afterwards.

答案2

得分: 0

R实际上尝试帮助您,但显示默认的小数位数,并提供不同的方法来影响它,正如您所看到的。

然而,有时候我们想要以一种"蛮力方式"来覆盖这个设置。一种可能的方式是使用round()函数:

> matrix(c(0.00003,0.1111111),ncol=1)   # 你的示例
         [,1]
[1,] 0.000030
[2,] 0.111111
> 
> round(matrix(c(0.00003,0.1111111),ncol=1), digits=3)
      [,1]
[1,] 0.000
[2,] 0.111
> 

如果您想要自动化这个过程,您可以创建一个新类型myMatrix,它继承自matrix,并覆盖其print方法,使其按照这里所示的方式进行四舍五入。

英文:

R actually tries to help you here but showing the default number of digits, and offers different ways to influence this as you saw.

However, sometimes we want to override this in a 'brute force way'. One possible way is to round():

&gt; matrix(c(0.00003,0.1111111),ncol=1)   # your example
         [,1]
[1,] 0.000030
[2,] 0.111111
&gt;
&gt; round(matrix(c(0.00003,0.1111111),ncol=1), digits=3)
      [,1]
[1,] 0.000
[2,] 0.111
&gt; 

If you want to automate this you could create a new type myMatrix which inherits from matrix, and overwrites its print method by one that rounds as shown here.

huangapple
  • 本文由 发表于 2023年5月21日 22:13:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300330.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定