[R] Peculiar behaviour subtracting from named numeric vector

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Tue Oct 18 21:14:01 CEST 2005


Hi, Rich,

This comes up so much it is FAQ 7.31.

http://cran.r-project.org/doc/FAQ/R-FAQ.html

--sundar

Rich Grenyer wrote:
> Hi - I'm just puzzling over something R's been doing when subtracting a 
> constant from a named numeric vector. I hope this isn't anything 
> embarrassingly simple, but I've tried and failed the usual searches. 
> For the last item in my vector, 40-21=19, but 40-40 = 7.105E-15 . If I 
> replace the last item of the vector explicitly with 40 (i.e. 
> goodtimees[21]<-40), then 40-40=0. I presume that this means the last 
> item in the vector is not 40, but 40 + 7.105E-15. This is possible 
> (goodtimees is the output of a function in an R addon package - it 
> should be at the same precision as the input to that function, but 
> there could be a problem with it, perhaps) but either way, why isn't 
> the true value showing when the vector is displayed? Is there a more 
> deep-rooted problem?
> 
> Thanks in advance...
> 
> Rich
> 
> 
>  > goodtimees
>    -1   -2   -3   -4   -5   -6   -7   -8  -28  -48  -93  -96 -152 -163 
> -164 -167 -168 -169 -177 -188 -189
> 98.1 86.8 77.9 76.3 70.6 61.1 53.6 51.5 47.5 43.8 41.7 72.3 41.8 65.3 
> 49.4 61.1 54.9 43.0 44.9 53.0 40.0
> 
>  > str(goodtimees)
>   Named num [1:21] 98.1 86.8 77.9 76.3 70.6 ...
>   - attr(*, "names")= chr [1:21] "-1" "-2" "-3" "-4" ...
> 
>  > goodtimees-21
>    -1   -2   -3   -4   -5   -6   -7   -8  -28  -48  -93  -96 -152 -163 
> -164 -167 -168 -169 -177 -188 -189
> 77.1 65.8 56.9 55.3 49.6 40.1 32.6 30.5 26.5 22.8 20.7 51.3 20.8 44.3 
> 28.4 40.1 33.9 22.0 23.9 32.0 19.0
> 
>  > goodtimees-40
>            -1           -2           -3           -4           -5        
>     -6           -7           -8
> 5.810000e+01 4.680000e+01 3.790000e+01 3.630000e+01 3.060000e+01 
> 2.110000e+01 1.360000e+01 1.150000e+01
>           -28          -48          -93          -96         -152        
>   -163         -164         -167
> 7.500000e+00 3.800000e+00 1.700000e+00 3.230000e+01 1.800000e+00 
> 2.530000e+01 9.400000e+00 2.110000e+01
>          -168         -169         -177         -188         -189
> 1.490000e+01 3.000000e+00 4.900000e+00 1.300000e+01 7.105427e-15
> 
>  > version
>           _
> platform powerpc-apple-darwin7.9.0
> arch     powerpc
> os       darwin7.9.0
> system   powerpc, darwin7.9.0
> status
> major    2
> minor    1.1
> year     2005
> month    06
> day      20
> language R
> 
> 
> 
> --------------------
> Rich Grenyer, Ph.D.
> Biology Department - University of Virginia
> Gilmer Hall
> Charlottesville, Virginia
> VA 22904
> United States of America
> 
> tel: (+1) 434 982 5629
> fax: (+1) 434 982 5626
> http://faculty.virginia.edu/gittleman/rich
> 
> ______________________________________________
> 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




More information about the R-help mailing list