[R] specify the number of decimal numbers

2009-05-14 Thread lehe
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:

Re: [R] specify the number of decimal numbers

2009-05-14 Thread jim holtman
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

Re: [R] specify the number of decimal numbers

2009-05-14 Thread baptiste auguie
Alternatively, signif(c(pi,exp(1)), 3) ?signif # and others in that page HTH, baptiste On 14 May 2009, at 13:47, jim holtman 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

Re: [R] specify the number of decimal numbers

2009-05-14 Thread Ted Harding
On 14-May-09 11:40:21, lehe 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! Since you say you

Re: [R] specify the number of decimal numbers

2009-05-14 Thread lehe
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 -

Re: [R] specify the number of decimal numbers

2009-05-14 Thread jim holtman
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

Re: [R] specify the number of decimal numbers

2009-05-14 Thread Wacek Kusnierczyk
jim holtman 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 ... but remember that sprintf introduces excel bugs into r

Re: [R] specify the number of decimal numbers

2009-05-14 Thread lehe
Thanks to all of you! Yes, I am generating a latex table in my report. jholtman wrote: 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

Re: [R] specify the number of decimal numbers

2009-05-14 Thread Ted Harding
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

Re: [R] specify the number of decimal numbers

2009-05-14 Thread Ted Harding
On 14-May-09 12:27:40, Wacek Kusnierczyk wrote: jim holtman 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 ... but remember that sprintf introduces

Re: [R] specify the number of decimal numbers

2009-05-14 Thread Wacek Kusnierczyk
(Ted Harding) wrote: On 14-May-09 12:27:40, Wacek Kusnierczyk wrote: ... but remember that sprintf introduces excel bugs into r (i.e., rounding is not done according to the IEC 60559 standard, see ?round): ns = c(0.05, 0.15) round(ns, 1) # 0.0 0.2

Re: [R] specify the number of decimal numbers

2009-05-14 Thread James W. MacDonald
Wacek Kusnierczyk wrote: (Ted Harding) wrote: On 14-May-09 12:27:40, Wacek Kusnierczyk wrote: ... but remember that sprintf introduces excel bugs into r (i.e., rounding is not done according to the IEC 60559 standard, see ?round): ns = c(0.05, 0.15) round(ns, 1) # 0.0 0.2

Re: [R] specify the number of decimal numbers

2009-05-14 Thread Wacek Kusnierczyk
James W. MacDonald wrote: Wacek Kusnierczyk wrote: (Ted Harding) wrote: On 14-May-09 12:27:40, Wacek Kusnierczyk wrote: ... but remember that sprintf introduces excel bugs into r (i.e., rounding is not done according to the IEC 60559 standard, see ?round): ns = c(0.05, 0.15)

Re: [R] specify the number of decimal numbers

2009-05-14 Thread James W. MacDonald
Wacek Kusnierczyk wrote: do they make pompous claims about their software and disregarding claims about others' as well? My mistake. I thought your concern was for the quality of the software (quality of course being defined by a certain committee of one). But it appears this is of a more

Re: [R] specify the number of decimal numbers

2009-05-14 Thread Wacek Kusnierczyk
James W. MacDonald wrote: Wacek Kusnierczyk wrote: do they make pompous claims about their software and disregarding claims about others' as well? My mistake. I thought your concern was for the quality of the software (quality of course being defined by a certain committee of one). But it

Re: [R] specify the number of decimal numbers

2009-05-14 Thread Ted Harding
On 14-May-09 15:15:16, James W. MacDonald wrote: Wacek Kusnierczyk wrote: (Ted Harding) wrote: On 14-May-09 12:27:40, Wacek Kusnierczyk wrote: ... but remember that sprintf introduces excel bugs into r (i.e., rounding is not done according to the IEC 60559 standard, see ?round):

Re: [R] specify the number of decimal numbers

2009-05-14 Thread Wacek Kusnierczyk
(Ted Harding) wrote: This happens also when you use C's fprintf and sprintf (at any rate in my gcc): r's printing routines (e.g., print, sprintf, cat, anything else?) seem to rely on the underlying c sprintf, with no prior r-implemented rounding. hence they import into r whatever