Skip to content
Prev 320235 / 398503 Next

Obtaining the internal integer codes of a factor XXXX

I would like to add that using the labels argument without a levels
argument in factor(),
   fdata<-factor(c("b","b","c","a","b","c"),labels=c("I","II","III"))
is a dangerous way to make your factor.

Consider what would happen if the 4th input value were not "a" but
was "d" or if you were in a locale where the sort order is not "a", "b",
"c" (this is unlikely in this example, but if you had some capitalized and
some not the sort order would be different in the "C" locale and almost
any other locale).

Avoid the problem by stating what the input levels are expected to be:
   fdata <- factor( c("b","b","c","a","b","c"), levels=c("a","b","c"), labels=c("I","II","III"))

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com