Re: [R] Automatic rounding of values after factors , converted to numeric, are multipled by a real number
On Thu, 2005-10-20 at 16:20 +0200, Peter Dalgaard wrote: > "Nelson, Gary (FWE)" <[EMAIL PROTECTED]> writes: > > > Peter, > > > > Thank you for your response. I knew how close the values are to > > integers, but I still don't understand why I don't have control over > > how the numbers are displayed (rounded or not)? > > It's all in the conventions. The digits in print() and friends are > significant digits, so we first round to that many significant digits, > then discard trailing zeros, which is why > > > 12.51 > [1] 12.5 > > 12.50001 > [1] 12.50001 > > The exception is that we do not discard significant digits to the left > of the decimal point, unless we are using scientific notation > > > print(12345678,digits=2) > [1] 1.2e+07 > > print(12345678,digits=5) > [1] 12345678 > > (the "scipen" options controls the logic for switching notation). > > For finer control we have the formatC function: > > > format(1234.1,digits=9) # same thing as with print() > [1] "1234.1" > > format(1234.1,digits=8) > [1] "1234" > > formatC(1234.1,digits=5,format="f") > [1] "1234.1" > > formatC(1234.1,digits=4,format="f") > [1] "1234." Also, sprintf(): > sprintf("%.9f", 1234.1) [1] "1234.1" > sprintf("%.4f", 1234.1) [1] "1234." > sprintf("%12.4f", 1234.1) [1] " 1234." HTH, Marc Schwartz __ R-help@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
Re: [R] Automatic rounding of values after factors , converted to numeric, are multipled by a real number
"Nelson, Gary (FWE)" <[EMAIL PROTECTED]> writes: > Peter, > > Thank you for your response. I knew how close the values are to > integers, but I still don't understand why I don't have control over > how the numbers are displayed (rounded or not)? It's all in the conventions. The digits in print() and friends are significant digits, so we first round to that many significant digits, then discard trailing zeros, which is why > 12.51 [1] 12.5 > 12.50001 [1] 12.50001 The exception is that we do not discard significant digits to the left of the decimal point, unless we are using scientific notation > print(12345678,digits=2) [1] 1.2e+07 > print(12345678,digits=5) [1] 12345678 (the "scipen" options controls the logic for switching notation). For finer control we have the formatC function: > format(1234.1,digits=9) # same thing as with print() [1] "1234.1" > format(1234.1,digits=8) [1] "1234" > formatC(1234.1,digits=5,format="f") [1] "1234.1" > formatC(1234.1,digits=4,format="f") [1] "1234." -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@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
Re: [R] Automatic rounding of values after factors , converted to numeric, are multipled by a real number
Peter, Thank you for your response. I knew how close the values are to integers, but I still don't understand why I don't have control over how the numbers are displayed (rounded or not)? Thanks again, Gary Nelson. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Peter Dalgaard Sent: Wednesday, October 19, 2005 3:45 PM To: Nelson, Gary (FWE) Cc: r-help@stat.math.ethz.ch Subject: Re: [R] Automatic rounding of values after factors , converted to numeric, are multipled by a real number "Nelson, Gary (FWE)" <[EMAIL PROTECTED]> writes: > I am wondering if someone would have any suggestion about my issue? > > > I have the following code: > > wgts<-aggregate(subset(lendata,select=c(Length)),list(lendata$Cruise,l > en > data$Station,lendata$Region,lendata$Total),mean) > wgts<-wgts[order(wgts$Group.3,wgts$Group.1,wgts$Group.1),] > names(wgts)<-c("Cruise","Station","Region","Total","MLen") > wgts$Total<-as.numeric(levels(wgts$Total))[wgts$Total] > wgts$swmean<-with(wgts,wgts$Total*wgts$MLen) > > When I run it, I get: >Cruise Station Region Total MLen swmean > 32350 256 1 2 70.5141 > 52350 254 1 3 73.3220 > 62350 287 1 3 65.7197 > 92350 232 1 4 75.25000301 > 10 2350 294 1 4 56.0224 > 12 2350 301 1 5 70.2351 > 14 2350 316 1 6 67.3404 > 15 2350 255 1 7 55.0385 > 17 2350 285 1 8 73.5588 > 19 2350 212 110 57.5575 > 20 2350 250 110 61.5615 > 27 2350 221 124 95.29167 2287 > 33 2350 229 135 55.62857 1947 > 37 2350 293 147 53.82979 2530 > 38 2350 203 150 55.54000 2777 > 39 2350 248 155 63.30909 3482 > 41 2350 246 163 95.82540 6037 > 42 2350 265 168 55.54412 3777 > 43 2350 251 182 62.60976 5134 > 44 2350 234 185 57.21176 4863 > > Every value is correct except that the "swmean"s are rounded and I > can't get values with the decimals fractions. I have tried as.double > and have change the options(digits=7), but nothing seems to work. I > have spent several hours combing manuals and archives. as far as I can see, the issue is that Total*MLen just *are* pretty close to being integers, e.g. > 4863/85 [1] 57.21176 > z <- x$Total*x$MLen > z - round(z) [1] 0.0 -0.1 0.1 0.0 0.0 0.0 -0.2 0.0 [9] 0.0 0.0 0.0 0.8 -0.5 0.00013 0.0 -0.5 [17] 0.00020 0.00016 0.00032 -0.00040 -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@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
Re: [R] Automatic rounding of values after factors , converted to numeric, are multipled by a real number
"Nelson, Gary (FWE)" <[EMAIL PROTECTED]> writes: > I am wondering if someone would have any suggestion about my issue? > > > I have the following code: > > wgts<-aggregate(subset(lendata,select=c(Length)),list(lendata$Cruise,len > data$Station,lendata$Region,lendata$Total),mean) > wgts<-wgts[order(wgts$Group.3,wgts$Group.1,wgts$Group.1),] > names(wgts)<-c("Cruise","Station","Region","Total","MLen") > wgts$Total<-as.numeric(levels(wgts$Total))[wgts$Total] > wgts$swmean<-with(wgts,wgts$Total*wgts$MLen) > > When I run it, I get: >Cruise Station Region Total MLen swmean > 32350 256 1 2 70.5141 > 52350 254 1 3 73.3220 > 62350 287 1 3 65.7197 > 92350 232 1 4 75.25000301 > 10 2350 294 1 4 56.0224 > 12 2350 301 1 5 70.2351 > 14 2350 316 1 6 67.3404 > 15 2350 255 1 7 55.0385 > 17 2350 285 1 8 73.5588 > 19 2350 212 110 57.5575 > 20 2350 250 110 61.5615 > 27 2350 221 124 95.29167 2287 > 33 2350 229 135 55.62857 1947 > 37 2350 293 147 53.82979 2530 > 38 2350 203 150 55.54000 2777 > 39 2350 248 155 63.30909 3482 > 41 2350 246 163 95.82540 6037 > 42 2350 265 168 55.54412 3777 > 43 2350 251 182 62.60976 5134 > 44 2350 234 185 57.21176 4863 > > Every value is correct except that the "swmean"s are rounded and I can't > get values with the decimals fractions. I have tried as.double and have > change the options(digits=7), but nothing seems to work. I have spent > several hours combing manuals and archives. as far as I can see, the issue is that Total*MLen just *are* pretty close to being integers, e.g. > 4863/85 [1] 57.21176 > z <- x$Total*x$MLen > z - round(z) [1] 0.0 -0.1 0.1 0.0 0.0 0.0 -0.2 0.0 [9] 0.0 0.0 0.0 0.8 -0.5 0.00013 0.0 -0.5 [17] 0.00020 0.00016 0.00032 -0.00040 -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@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