[Rd] Match .3 in a sequence

Petr Savicky savicky at cs.cas.cz
Tue Mar 17 11:10:28 CET 2009


On Tue, Mar 17, 2009 at 10:15:39AM +0100, Wacek Kusnierczyk wrote:
...
> there's one more curiosity about factors, in particular, ordered factors:
> 
>     ord <- as.ordered(nums); ord
>     # [1] 0.300000000000000 0.3               0.3              
> 0.300000000000000
>     # Levels: 0.300000000000000 < 0.3 < 0.3 < 0.300000000000000
> 
>     ord[1] < ord[4]
>     # TRUE
>     ord[1] == ord[4]
>     # TRUE
...

The following workaround should help in most cases.

  nums <- (.3 + 2e-16 * c(-2,-1,1,2))
  nums # [1] 0.3 0.3 0.3 0.3
  nums <- signif(nums, digits=15)
  as.ordered(nums) # Levels: 0.3
  as.factor(nums) # Levels: 0.3

Petr.



More information about the R-devel mailing list