[R] Finicky factor comparison operators
johnmark
johnmark.agosta at gmail.com
Mon Feb 20 07:45:40 CET 2012
MIchael -
Thanks for your insight. I think I see where you're going with this.
To make '==' comparisons for subsetting against an ordered factor, I've had
to create a lookup table for all possible values I'd ever want to compare
against (all dates covered by the quarters in question, in this case) that
maps into the ordered factors values. This is wrapped by a function that
returns an ordered factor, which allows me to write:
/(opps$close_quarter == which.quarter.end("2010-10-20")/
Otherwise if I try to create an ordered factor from the constant just for
the purposes of comparison, the error tells me that ordered factors from
different sources cannot be compared:
/(opps$close_quarter == factor("2007-10-20", ordered=T)
Error in Ops.factor(factor("2007-10-30", ordered = T), quarter.factors[1,
2]) :
level sets of factors are different/
That makes sense, since internally factors are integers -- "enums" in other
terms.
But what I want to avoid -- and what I don't see as necessary is explicitly
coercing the terms to a common representation that mimics their print form:
/as.character("2007-10-20")== as.character(factor("2007-10-20", ordered=T))
/
I don't think there should be confusion since the conversion to print form
is "obvious" -- but it does conflict with the conversion rules for creating
vectors by c():
/c("2011-10-20", factor("2007-10-20", ordered=T))
[1] "2011-10-20" "1" /
where the factor is converted to its internal "enum" representation, then to
a character.
Having given this some more thought to what motivated the original question,
one could use "which()" to invert the factor's levels vector:
/which("2008-04-30" == levels(quarter.factors[,2]))
[1] 3 /
Its still not clear to me what exactly are the implicit conversion rules for
factors.
Cheers -jm
/
--
View this message in context: http://r.789695.n4.nabble.com/Finicky-factor-comparison-operators-tp4400377p4403352.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list