[R] Automatic rounding of values after factors , converted to numeric, are multipled by a real number

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Oct 19 22:45:13 CEST 2005


"Nelson, Gary (FWE)" <Gary.Nelson at state.ma.us> 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
> 3    2350     256      1     2 70.50000    141
> 5    2350     254      1     3 73.33333    220
> 6    2350     287      1     3 65.66667    197
> 9    2350     232      1     4 75.25000    301
> 10   2350     294      1     4 56.00000    224
> 12   2350     301      1     5 70.20000    351
> 14   2350     316      1     6 67.33333    404
> 15   2350     255      1     7 55.00000    385
> 17   2350     285      1     8 73.50000    588
> 19   2350     212      1    10 57.50000    575
> 20   2350     250      1    10 61.50000    615
> 27   2350     221      1    24 95.29167   2287
> 33   2350     229      1    35 55.62857   1947
> 37   2350     293      1    47 53.82979   2530
> 38   2350     203      1    50 55.54000   2777
> 39   2350     248      1    55 63.30909   3482
> 41   2350     246      1    63 95.82540   6037
> 42   2350     265      1    68 55.54412   3777
> 43   2350     251      1    82 62.60976   5134
> 44   2350     234      1    85 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.00000 -0.00001  0.00001  0.00000  0.00000  0.00000 -0.00002  0.00000
 [9]  0.00000  0.00000  0.00000  0.00008 -0.00005  0.00013  0.00000 -0.00005
[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
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list