typeof returns integer for something that is clearly a factor

后端 未结 2 488
悲哀的现实
悲哀的现实 2020-12-03 14:51

Create a variable:

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

Check type:

typeof(a_variable)

I want a factor -

2条回答
  •  無奈伤痛
    2020-12-03 15:53

    This is a language feature that confused me as well in my early days of R programming. The typeof function is giving information that's at a "lower" level of abstraction. Factor variables (and also Dates) 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 full "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 values are actually stored in an attribute (which is what "levels" returns), while the "main" part of the variable is a set of integer indices pointing to teh various level "attributes), named .Label, so mode returns "numeric" and typeof returns "integer". For this reason one usually needs to use as.character that will coerce to what most people think of as factors, namely their character representations.

提交回复
热议问题