Hello: I am generating a random number with rnorm(1). The generated number has 8 decimals. I don't want so many decimals. How to control the number of decimals in R? Thanks! Zhongmiao Wang
On 20 April 2006 at 22:00, zhongmiao wang wrote: | Hello: | I am generating a random number with rnorm(1). The generated number | has 8 decimals. I don't want so many decimals. How to control the | number of decimals in R? You must have installed the professional version of R, just downgrade to the home version which has only four digits precision. Seriously, your equating display precision with the representation -- which is not correct. Consider:> a <- 1/3 > print(a)[1] 0.3333333> print(a, digits=18)[1] 0.333333333333333315 where the second print statement forces a display beyond the number of significant digits. So with that, your rnorm(1) result will also have more than the eight digits you saw earlier:> print(rnorm(1), digits=18)[1] -0.201840514213267291 Hope this helps, Dirk -- Hell, there are no rules here - we're trying to accomplish something. -- Thomas A. Edison
You can also try: round(runif(1)*10^4)/10^4 --Brett -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Dirk Eddelbuettel Sent: Thursday, April 20, 2006 8:18 PM To: zhongmiao wang Cc: r-help at stat.math.ethz.ch Subject: Re: [R] how to control the data type On 20 April 2006 at 22:00, zhongmiao wang wrote: | Hello: | I am generating a random number with rnorm(1). The generated number | has 8 decimals. I don't want so many decimals. How to control the | number of decimals in R? You must have installed the professional version of R, just downgrade to the home version which has only four digits precision. Seriously, your equating display precision with the representation -- which is not correct. Consider:> a <- 1/3 > print(a)[1] 0.3333333> print(a, digits=18)[1] 0.333333333333333315 where the second print statement forces a display beyond the number of significant digits. So with that, your rnorm(1) result will also have more than the eight digits you saw earlier:> print(rnorm(1), digits=18)[1] -0.201840514213267291 Hope this helps, Dirk -- Hell, there are no rules here - we're trying to accomplish something. -- Thomas A. Edison ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Have a look at ?round for one way. ----- Original Message ---- From: zhongmiao wang <zhongmiao at gmail.com> To: r-help at stat.math.ethz.ch Sent: Thursday, April 20, 2006 11:00:25 PM Subject: [R] how to control the data type Hello: I am generating a random number with rnorm(1). The generated number has 8 decimals. I don't want so many decimals. How to control the number of decimals in R? Thanks! Zhongmiao Wang ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reasonably Related Threads
- what is the difference between survival analysis and logistic regression with a timing variable?
- A question about nlme
- round function seems to produce maximum 2 decimals
- unexpected behavior using round to 2 digits on randomly generated numbers
- decimal troubles ?