Yesterday I was biten by a feature, which I find too dangerous. 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)??. This will not cause a semantic error, when the levels of Subject happen to be in numeric order; so it will be unnoticed quite often. I propose to issue a warning whenever a factor is implicitly coerced, at least in an ambiguous context.
This *is* a documented feature: see ?Extract. I am afraid your `of course' is completely non-obvious to me. I would object strongly to code written to use a documented feature issuing a warning. On Tue, 20 Apr 2004, Wilhelm B. Kloke wrote:> Yesterday I was biten by a feature, which I find too dangerous. > > 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)??. This will not cause a semantic error, when > the levels of Subject happen to be in numeric order; so it will be > unnoticed quite often. > > I propose to issue a warning whenever a factor is implicitly coerced, > at least in an ambiguous context.-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
> Yesterday I was biten by a feature, which I find too dangerous.>From your description, it sounds like you confused row lables and vectorvalues -- 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
Apparently Analagous Threads
- POSIXct-coerced NA's not considered NA by is.na()
- How to get warning about implicit factor to integer coercion?
- [LLVMdev] Dealing with a corrupted /proc/self/exe link
- [LLVMdev] [llvm-commits] Dealing with a corrupted /proc/self/exe link
- [LLVMdev] [llvm-commits] Dealing with a corrupted /proc/self/exe link