Fernando Henrique Ferraz Pereira da Rosa
2003-Jan-30 00:29 UTC
[R] Weird options(digits=n) behaviour
I noticed some very weird behaviour of the function: options(digits=n), where n is the number of digits you would expect to get in R calculations. Let's take a example:> options(digits=4) > getdata(caso.pool.k3.r3.e2)[1] 6.053 2.641 -3.639 14.259 6.082 Which works fine... now, trying again, with different data:> options(digits=4) > getdata(controle.pool.k3.r3.e2)[1] -0.03091 1.60310 -4.90588 5.07379 -0.04418>Which gives me 6 digits instead of 6. If I try digits=2, it then works with this data:> options(digits=2) > getdata(controle.pool.k3.r3.e2)[1] -0.031 1.603 -4.906 5.074 -0.044 But not with the first one:> getdata(caso.pool.k3.r3.e2)[1] 6.1 2.6 -3.6 14.3 6.1 getdata source code is as folllows: function(v) { o <- c(mean(v),sd(v),min(v),max(v),median(v)) o } What is going on? --
Fernando Henrique Ferraz Pereira da Rosa <mentus at gmx.de> writes:> I noticed some very weird behaviour of the function: options(digits=n), > where n is the number of digits you would expect to get in R calculations. > Let's take a example: > > > options(digits=4) > > getdata(caso.pool.k3.r3.e2) > [1] 6.053 2.641 -3.639 14.259 6.082 > > Which works fine... now, trying again, with different data: > > > options(digits=4) > > getdata(controle.pool.k3.r3.e2) > [1] -0.03091 1.60310 -4.90588 5.07379 -0.04418 > > > Which gives me 6 digits instead of 6. If I try digits=2, it then works > with this data:The 'digits' option sets the minimum number of significant digits. Some values in that array are printed to 6 significant digits but the first and last have only 4 significant digits.
Fernando Henrique Ferraz Pereira da Rosa <mentus at gmx.de> writes:> > options(digits=4)> [1] 6.053 2.641 -3.639 14.259 6.082> [1] -0.03091 1.60310 -4.90588 5.07379 -0.04418> > options(digits=2) > [1] -0.031 1.603 -4.906 5.074 -0.044> [1] 6.1 2.6 -3.6 14.3 6.1> What is going on?This is normal. digits=4 means that you need at least four significant digits of the result. When you print vectors all values get printed in the same format. Notice that in the second case -0.03091 has 4 significant digits and -0.04418 ditto and in the 3rd case likewise. 6.1 has two significant digits by the same conventions. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907