I have a factor RFyhat which I’m looking to convert to a numeric vector. I’ve already discovered that
as.numeric(levels(RFyhat))[RFyhat]
works as desired, and I’ve played around a bit with this construction:
c(1,2,20,4,5,6,7)[RFyhat]
also works as expected (RFyhat has 7 levels).
So I understand the behavior of this construction, but I’m wondering if anyone can explain how this syntax is intended to work, or whether it is just ‘syntactic sugar’. More specifically, does [RFyhat] act as an index vector? If it does, how do factors generally behave when used as an index?
Yes, I believe that factors gets converted to integers when used for indexing, rather than characters or anything else.
Look at this example
So element 1 of
fachas returned element 1 ofvec, regardless of the different order of names.Personally I’d prefer
as.integer(as.character(RFyhat))toas.numeric(levels(RFyhat))[...].