Skip to content

Factor to numeric conversion - as.numeric(as.character(f))[f] - Language definition seems to say to not use this.

5 messages · Matthew Lundberg, Ista Zahn, Peter Ehlers +1 more

#
Hi Mathew,

In what way are they at odds?

On Mon, Apr 1, 2013 at 1:48 PM, Matthew Lundberg
<matthew.k.lundberg at gmail.com> wrote:
#
On 2013-04-01 10:48, Matthew Lundberg wrote:
Hint:

  f <- factor(sample(5, 10, TRUE))
  as.numeric(levels(f))[f]

  g <- factor(sample(letters[1:5], 10, TRUE))
  as.numeric(levels(g))[g]

Peter Ehlers
#
Yup. Note also:
function (x, ...)
levels(x)[x]

But of course this is OK, since this can change if the implementation
does. Which is the whole point, of course.

-- Bert



On Mon, Apr 1, 2013 at 12:16 PM, Matthew Lundberg
<matthew.k.lundberg at gmail.com> wrote:
--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm