It all depends on what you want to do with the result.  Here are some
variations:

> x <- matrix(runif(16), 4)
> x
          [,1]      [,2]       [,3]      [,4]
[1,] 0.2655087 0.2016819 0.62911404 0.6870228
[2,] 0.3721239 0.8983897 0.06178627 0.3841037
[3,] 0.5728534 0.9446753 0.20597457 0.7698414
[4,] 0.9082078 0.6607978 0.17655675 0.4976992
> x[] <- sprintf("%.3f", x)
> x
     [,1]    [,2]    [,3]    [,4]
[1,] "0.266" "0.202" "0.629" "0.687"
[2,] "0.372" "0.898" "0.062" "0.384"
[3,] "0.573" "0.945" "0.206" "0.770"
[4,] "0.908" "0.661" "0.177" "0.498"
> print(x, quote=FALSE)
     [,1]  [,2]  [,3]  [,4]
[1,] 0.718 0.935 0.267 0.870
[2,] 0.992 0.212 0.386 0.340
[3,] 0.380 0.652 0.013 0.482
[4,] 0.777 0.126 0.382 0.600

> x <- matrix(runif(16), 4)
> signif(x,3)
      [,1]  [,2]   [,3]  [,4]
[1,] 0.718 0.935 0.2670 0.870
[2,] 0.992 0.212 0.3860 0.340
[3,] 0.380 0.652 0.0134 0.482
[4,] 0.777 0.126 0.3820 0.600
>

Can you specify what you want and how are you going to use it.  Is it for
generating a report?
On Thu, May 14, 2009 at 8:03 AM, lehe <timlee...@yahoo.com> 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?
>
>
> 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 <timlee...@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-tp23538852p23538852.html
> >> Sent from the R help mailing list archive at Nabble.com.
> >>
> >> ______________________________________________
> >> R-help@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/posting-guide.html>
> <http://www.r-project.org/posting-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@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/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-tp23538852p23539189.html
>  Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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/posting-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@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.

Reply via email to