From savicky at cs.cas.cz  Mon May 11 21:03:52 2009
From: savicky at cs.cas.cz (Petr Savicky)
Date: Mon, 11 May 2009 21:03:52 +0200
Subject: [Rd] suggestion for extending ?as.factor
In-Reply-To: <18952.16126.640019.723302@lynne.math.ethz.ch>
References: <49FF25A6.3070907@biostat.ku.dk>
	<18943.64094.648164.476070@lynne.math.ethz.ch>
	<18945.19798.782710.936788@lynne.math.ethz.ch>
	<20090508090155.GA18486@cs.cas.cz>
	<18948.12553.265256.981657@lynne.math.ethz.ch>
	<20090508151448.GA6692@cs.cas.cz> <20090508161056.GB6692@cs.cas.cz>
	<18949.60853.709036.388905@cmath-5.math.ethz.ch>
	<20090510115253.GA26160@cs.cas.cz>
	<18952.16126.640019.723302@lynne.math.ethz.ch>
Message-ID: <20090511190352.GA13229@cs.cas.cz>

On Mon, May 11, 2009 at 05:06:38PM +0200, Martin Maechler wrote:
[...]
> The version I have committed a few hours ago is indeed a much
> re-simplified version, using  as.character(.) explicitly
> and consequently no longer providing the extra optional
> arguments that we have had for a couple of days.
> 
> Keeping such a basic function   factor()  as simple as possible 
> seems a good strategy to me.

OK. I understand the argument of simplicity. So, factor(x) is just
a compressed encoding of as.character(x), where each value is stored
only once. This sounds good to me.

Let me go back to the original purpose of this thread: suggestion for
extending ?as.factor

I think that somewhere in the help page, we could have something like

  Using factor() to a numeric vector should be done with caution. The
  information in x is preserved to the extent to which it is preserved
  in as.character(x). If this leads to too many different levels due to minor
  differences among the input numbers, it is suggested to use something like 
  factor(signif(x, digits)) or factor(round(x, digits)), where the number of 
  decimal digits appropriate for a given application should be used.

Let me point out that the following sentence from Warning is not exactly correct
as it is in svn at the moment. So, i suggest to add the word "approximately" to
the place marked with square brackets and add one more sentence of explanation
marked also by square brackets.

  To transform a factor \code{f} to [approximately]
  its original numeric values, \code{as.numeric(levels(f))[f]} is
  recommended and slightly more efficient than
  \code{as.numeric(as.character(f))}.
  [Note that the original values may be extracted only to the precision
  used in as.character(x), which is typically 15 decimal digits.]

Petr.



[Rd] suggestion for extending ?as.factor
Petr Savicky