2

How do I get the mode of the underlying values in a factor? For example, given test2 below, how would I get "character" instead of "numeric"?

test = c( "a" , "b" , "c" )
mode( test ) # "character"
test2 = factor( test )
mode( test2 ) # "numeric"
smci
  • 32,567
  • 20
  • 113
  • 146
Suraj
  • 35,905
  • 47
  • 139
  • 250

2 Answers2

4

Use

 mode(levels(test2))

to test the factor's levels rather than values.

You can think of a factor as a hashed or keyed variable: you simply get numerical indices that you'd use to index in the map from numeric value to textual labels. In that view, it is clear that you wanted to test the mode of the labels rather than values.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • This is wrong because *`mode()`* gives storage mode, not the statistical measure. See [this question: 'Standard library function in R for finding the mode?'](http://stackoverflow.com/questions/2547402/standard-library-function-in-r-for-finding-the-mode). – smci Oct 26 '11 at 19:23
  • Ooops. Thanks for catching that. I guess John already reported the same on the same day... – Dirk Eddelbuettel Oct 26 '11 at 20:21
3

The mode function returns the storage mode. Factors are stored internally as integers (numeric) and have levels (the "a","b","c" in your example). The levels are characters. A common idiom with factors is to coerce them to character, which does this:

> as.character.factor
function (x, ...) 
levels(x)[x]
<environment: namespace:base>
jverzani
  • 5,600
  • 2
  • 21
  • 17