Convert NA into a factor level

前端 未结 3 1060
感动是毒
感动是毒 2020-11-28 07:12

I have a vector with NA values that I would like to replace by a new factor level NA.

a = as.factor(as.character(c(1, 1, 2, 2, 3, N         


        
相关标签:
3条回答
  • 2020-11-28 07:54

    You can add the NA as a level and change the level name to something more explicit than <NA> using fct_explicit_na from package forcats.

    library(forcats)
    

    By default you get the new level as (Missing):

    fct_explicit_na(a)
    
    [1] 1         1         2         2         3         (Missing)
    Levels: 1 2 3 (Missing)
    

    You can set it to something else:

    fct_explicit_na(a, "unknown")
    
    [1] 1       1       2       2       3       unknown
    Levels: 1 2 3 unknown
    
    0 讨论(0)
  • 2020-11-28 07:59

    Set the exclude argument to NULL to include NAs as levels (and use factor instead of as.factor. Does the same thing and has more arguments to set):

    a = factor(as.character(c(1, 1, 2, 2, 3, NA)), exclude = NULL)
    
    > a
    [1] 1    1    2    2    3    <NA>
    Levels: 1 2 3 <NA>
    
    0 讨论(0)
  • 2020-11-28 08:11

    You can use addNA().

    x <- c(1, 1, 2, 2, 3, NA)
    addNA(x)
    # [1] 1    1    2    2    3    <NA>
    # Levels: 1 2 3 <NA>
    

    This is basically a convenience function for factoring with exclude = NULL. From help(factor) -

    addNA modifies a factor by turning NA into an extra level (so that NA values are counted in tables, for instance).

    So another reason this is nice is because if you already have a factor f, you can use addNA() to quickly add NA as a factor level without changing f. As mentioned in the documentation, this is handy for tables. It also reads nicely.

    0 讨论(0)
提交回复
热议问题