[Rd] Match .3 in a sequence

Stavros Macrakis macrakis at alum.mit.edu
Tue Mar 17 15:04:39 CET 2009


Petr,

Thank you for the detailed diagnosis of the bizarre behavior I
reported, which seems to indicate several distinct problems in the
underlying code:

1) Factor allows repeated levels, e.g. factor(c(1),c(1,1,1)), with no
warning or error.

2) Even from distinct inputs, factor of a numeric vector may generate
repeated levels, because it only uses 15 digits.

3) The algorithm to determine the shortest format is inconsistent with
the algorithm to actually print, giving pathological cases like 0.3
vs. 0.300000000000000.

About:

> In order to avoid using 15 digits in as.character(), we can use round(,digits),
> with digits argument appropriate for the current situation.
>
>  > fact <- as.factor(round(nums,digits=1)); fact
>  [1] 0.3 0.3 0.3 0.3
>  Levels: 0.3

The original problem was testing whether a floating-point number was a
member of a vector.  rounding and then converting to a factor seem
like a very poor way of doing that, even if the above problems were
resolved.  Comparing with a tolerance seems much more robust, clean,
and efficient.

           -s



More information about the R-devel mailing list