[R] Indexing by factor misfeature

Jason Turner jasont at indigoindustrial.co.nz
Tue Apr 20 11:04:57 CEST 2004


> Yesterday I was biten by a feature, which I find too dangerous.

>From your description, it sounds like you confused row lables and vector
values -- two very different things.  It also sounds like you mixed up
factor values and levels.  Let me know if I'm wrong.

> I wanted to use a factor `Subject´ as index into a data frame, whose row
> names were the levels of this factor.
> So there a 2 different possible
> interpretations of this: Either Subject is coerced to numeric or to
> character. The intended interpretation was, of course,
> `as.character(Subject)'.
> R did `as.numeric(Subject)´.

What did you do to make R do this coersion?  I can't tell from here.

Try this, with the same "Subject" vector:

foo <- factor(Subject)
foo
levels(foo) <- names(Subject)
foo

Does that help?

Cheers

Jason




More information about the R-help mailing list