Search code examples
rdataframeapplyfactorsr-factor

R: Why am I not getting type or class "factor" after converting columns to factor?


I have the following setup.

df <- data.frame(aa = rnorm(1000), bb = rnorm(1000))

apply(df, 2, typeof)
#      aa       bb 
#"double" "double" 

apply(df, 2, class)
#       aa        bb 
#"numeric" "numeric" 

Then I try to convert one of the columns to "factor". But as you can see below, I am not getting any "factor" type or classes. Am I doing anything wrong ?

df[, 1] <- as.factor(df[, 1])

apply(df, 2, typeof)
#         aa          bb 
#"character" "character" 

apply(df, 2, class)
#         aa          bb 
#"character" "character" 

Solution

  • Sorry I felt my original answer badly written. Why did I put that "matrix of factors" in the very beginning? Here is a better try.

    From ?apply:

     If ‘X’ is not an array but an object of a class with a non-null
     ‘dim’ value (such as a data frame), ‘apply’ attempts to coerce it
     to an array via ‘as.matrix’ if it is two-dimensional (e.g., a data
     frame) or via ‘as.array’.
    

    So a data frame is converted to a matrix by as.matrix, before FUN is applied row-wise or column-wise.

    From ?as.matrix:

     ‘as.matrix’ is a generic function.  The method for data frames
     will return a character matrix if there is only atomic columns and
     any non-(numeric/logical/complex) column, applying ‘as.vector’ to
     factors and ‘format’ to other non-character columns.  Otherwise,
     the usual coercion hierarchy (logical < integer < double <
     complex) will be used, e.g., all-logical data frames will be
     coerced to a logical matrix, mixed logical-integer will give a
     integer matrix, etc.
    
     The default method for ‘as.matrix’ calls ‘as.vector(x)’, and hence
     e.g. coerces factors to character vectors.
    

    I am not a native English speaker and I can't read the following (which looks rather important!). Can someone clarify it?

    The method for data frames will return a character matrix if there is only atomic columns and any non-(numeric/logical/complex) column, applying ‘as.vector’ to factors and ‘format’ to other non-character columns.

    From ?as.vector:

     Note that factors are _not_ vectors; ‘is.vector’ returns ‘FALSE’
     and ‘as.vector’ converts a factor to a character vector for ‘mode
     = "any"’.
    

    Simply put, as long as you have a factor column in a data frame, as.matrix gives you a character matrix.


    I believed this apply with data frame problem has been raised many times and the above just adds another duplicate answer. Really sorry. I failed to read OP's question carefully. What hit me in the first instance is that R can not build a true matrix of factors.

    f <- factor(letters[1:4])
    
    matrix(f, 2, 2)
    #     [,1] [,2]
    #[1,] "a"  "c" 
    #[2,] "b"  "d" 
    
    ## a sneaky way to get a matrix of factors by setting `dim` attribute
    dim(f) <- c(2, 2)
    #     [,1] [,2]
    #[1,] a    c   
    #[2,] b    d   
    #Levels: a b c d
    
    is.matrix(f)
    #[1] TRUE
    
    class(f)
    #[1] "factor"  ## not a true matrix with "matrix" class
    

    While this is interesting, it should be less-relevant to OP's question.

    Sorry again for making a mess here. So bad!!


    So if I do sapply would it help? Because I have many columns that need to be converted to factor.

    Use lapply actually. sapply would simplify the result to an array, which is a matrix in 2D case. Here is an example:

    dat <- head(trees)
    sapply(dat, as.factor)
    #     Girth  Height Volume
    #[1,] "8.3"  "70"   "10.3"
    #[2,] "8.6"  "65"   "10.3"
    #[3,] "8.8"  "63"   "10.2"
    #[4,] "10.5" "72"   "16.4"
    #[5,] "10.7" "81"   "18.8"
    #[6,] "10.8" "83"   "19.7"
    
    new_dat <- data.frame(lapply(dat, as.factor))
    str(new_dat)
    #'data.frame':  6 obs. of  3 variables:
    # $ Girth : Factor w/ 6 levels "8.3","8.6","8.8",..: 1 2 3 4 5 6
    # $ Height: Factor w/ 6 levels "63","65","70",..: 3 2 1 4 5 6
    # $ Volume: Factor w/ 5 levels "10.2","10.3",..: 2 2 1 3 4 5
    
    sapply(new_dat, class)
    #   Girth   Height   Volume 
    #"factor" "factor" "factor" 
    
    apply(new_dat, 2, class)
    #      Girth      Height      Volume 
    #"character" "character" "character" 
    

    Regarding typeof, factors are actually stored as integers.

    sapply(new_dat, typeof)
    #    Girth    Height    Volume 
    #"integer" "integer" "integer" 
    

    When you dput a factor you can see this. For example:

    dput(new_dat[[1]])
    #structure(1:6, .Label = c("8.3", "8.6", "8.8", "10.5", "10.7", 
    #"10.8"), class = "factor")
    

    The real values are 1:6. Character levels are just an attribute.