[R] specify the number of decimal numbers

(Ted Harding) Ted.Harding at manchester.ac.uk
Thu May 14 15:01:17 CEST 2009


On 14-May-09 12:03:43, lehe wrote:
> 
> Thanks! 
> In my case, I need to deal with a lot of such results, e.g. elements
> in a matrix. If using sprintf, does it mean I have to apply to each
> result individually? Is it possible to do it in a single command?

Yes, in various ways depending on how you want the output to be:

  M<-pi*matrix(c(1,2,3,4),nrow=2)
  M
#          [,1]      [,2]
# [1,] 3.141593  9.424778
# [2,] 6.283185 12.566371

  sprintf("%.3f",M)
# [1] "3.142"  "6.283"  "9.425"  "12.566"
  sprintf(c("%.1f","%.2f","%.3f","%.4f"),M)
# [1] "3.1"     "6.28"    "9.425"   "12.5664"

In this usage, the output is a vector of character strings.
If you wanted to keep the output character strings in matrix layout:

   matrix(sprintf("%.3f",M),nrow=2)
#      [,1]    [,2]    
# [1,] "3.142" "9.425" 
# [2,] "6.283" "12.566"

While one may think of using print() with the "digits" argument
for this kind of thing, "digits=3" for instance prints to a minimum
of 3 _significant_ digits, not decimal places. So:

  print(10*M,3)
#      [,1]  [,2]
# [1,] 31.4  94.2
# [2,] 62.8 125.7

See in ?print.default:
  The same number of decimal places is used throughout a vector. 
  This means that 'digits' specifies the minimum number of
  significant digits to be used, and that at least one entry will
  be encoded with that minimum number.

As far as I can tell, there is not a print option such as "decimals",
e.g. "decimals=3" which would print all numbers with 3 digits after
the decimal point (as in sprintf("%.3f",...)); if there were such
an option, one would expect to find it listed in ?options. If that
is really true, it is a pity! Short of using sprintf() (and then
converting back to numeric if you need the results as number), the
only way you could use "digits" to get a desired number (e.g. 3)
of decimal places throughout would be to first study the range of
magnitudes of the numbers and then choose "decimals=..." accordingly.
For example, to print 10*M to 2 d.p., you need to note that the
shortest number is 31.4***, so you need decimals=5 to get 3 d.p.:

  print(10*M,5)
#        [,1]    [,2]
# [1,] 31.416  94.248
# [2,] 62.832 125.664

On the other hand, you could do it straight off with sprintf:
[***]
  (matrix(as.numeric(sprintf("%.3f",10*M)),nrow=2))
#        [,1]    [,2]
# [1,] 31.416  94.248
# [2,] 62.832 125.664

regardless of the magnitudes of the numbers -- though you could
still fail if there are zeros after the decimal place:

[***]
  sprintf("%.3f",c(1.0,2.0,3.0,4.0))
# [1] "1.000" "2.000" "3.000" "4.000"
  as.numeric(sprintf("%.3f",c(1.0,2.0,3.0,4.0)))
# [1] 1 2 3 4

Maybe there really is a print method which will output a result
like [***] without the quotes; but I don't know how to find it!

ted.

> 
> 
> jholtman wrote:
>> 
>> Depending on what you want to do, use 'sprintf':
>> 
>>> x <- 1.23456789
>>> x
>> [1] 1.234568
>>> as.character(x)
>> [1] "1.23456789"
>>> sprintf("%.1f  %.3f  %.5f", x,x,x)
>> [1] "1.2  1.235  1.23457"
>>>
>> 
>> 
>> On Thu, May 14, 2009 at 7:40 AM, lehe <timlee126 at yahoo.com> wrote:
>> 
>>>
>>> Hi,
>>> I was wondering how to specify the number of decimal numbers in my
>>> computation using R? I have too many decimal numbers for my result,
>>> when
>>> I
>>> convert them to string with as.character, the string will be too
>>> long.
>>> Thanks and regards!
>>> --
>>> View this message in context:
>>> http://www.nabble.com/specify-the-number-of-decimal-numbers-tp23538852
>>> p23538852.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/p
>>> osting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>> 
>> 
>> 
>> -- 
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>> 
>> What is the problem that you are trying to solve?
>> 
>>      [[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>> 
>> 
> 
> -- 
> View this message in context:
> http://www.nabble.com/specify-the-number-of-decimal-numbers-tp23538852p2
> 3539189.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 14-May-09                                       Time: 14:01:14
------------------------------ XFMail ------------------------------




More information about the R-help mailing list