Rocinante Rocinante -4 years ago 97
R Question

Confused about types in R - typeof returns integer for something that is clearly a factor

Create a varaible

a_variable <- c("a","b","c")

Check type:


I want a factor - change to factor:

a_variable <- as.factor(a_variable)

Check type:


Says that it's integer!? As an R newb, this is confusing. I just told R to make a factor not an integer.

Test to see if it somehow magically did create an integer:

a_variable * 1

Hmm... I get an error message saying "*" isn't meaningful for factors. This seems weird to me since R just told me it was an integer!?

Clearly it's me who is confused, can someone more enlightened help make sense of this madness for me?

Answer Source

Factor variables are stored as integers. Learn to use class or str rather than typeof (or mode). They give more useful information. You can look at the structure of a factor variable with dput:

 dput( factor( rep( letters[1:5], 2) ) )
# structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), 
            .Label = c("a", "b", "c", "d", "e"), class = "factor")

The character values that are usually thought of as the factor items are actually stored in an attribute, while the "main" part of the variable is actually a set of integer indices into that attribute, named .Label, so mode returns "numeric" and typeof returns "integer". For this reason one usually to use as.character before coercion to other classes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download