to use ` [ is.na
](/link/is.na?package=base&version=3.6.2) ` on the left-hand-side of an
assignment (as in ` is.na(f)[i] <- TRUE ` ; indexing inside ` is.na ` does not
work). Under those circumstances missing values are currently printed as `
<NA> ` , i.e., identical to entries of level ` NA ` .

` is.factor ` is generic: you can write methods to handle specific classes of
objects, see [ InternalMethods
](/link/InternalMethods?package=base&version=3.6.2) .

Where ` levels ` is not supplied, ` [ unique
](/link/unique?package=base&version=3.6.2) ` is called. Since factors
typically have quite a small number of levels, for large vectors ` x ` it is
helpful to supply ` nmax ` as an upper bound on the number of unique values.

##  References

Chambers, J. M. and Hastie, T. J. (1992) _Statistical Models in S_ . Wadsworth
& Brooks/Cole.

##  See Also

` [ [.factor ](/link/%5B.factor?package=base&version=3.6.2) ` for subsetting
of factors.

` [ gl ](/link/gl?package=base&version=3.6.2) ` for construction of balanced
factors and ` [ C ](/link/C?package=base&version=3.6.2) ` for factors with
specified contrasts. ` [ levels ](/link/levels?package=base&version=3.6.2) `
and ` [ nlevels ](/link/nlevels?package=base&version=3.6.2) ` for accessing
the levels, and ` [ unclass ](/link/unclass?package=base&version=3.6.2) ` to
get integer codes.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Ffactor&utm_source=r-docs&utm_medium=docs&utm_term=factor&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    (ff <- factor(substring("statistics", 1:10, 1:10), levels = letters))
    as.integer(ff)      # the internal codes
    (f. <- factor(ff))  # drops the levels that do not occur
    ff[, drop = TRUE]   # the same, more transparently
    
    factor(letters[1:20], labels = "letter")
    
    class(ordered(4:1)) # "ordered", inheriting from "factor"
    z <- factor(LETTERS[3:1], ordered = TRUE)
    ## and "relational" methods work:
    stopifnot(sort(z)[c(1,3)] == range(z), min(z) < max(z))
    # }
    # NOT RUN {
    ## suppose you want "NA" as a level, and to allow missing values.
    (x <- factor(c(1, 2, NA), exclude = NULL))
    is.na(x)[2] <- TRUE
    x  # [1] 1    <NA> <NA>
    is.na(x)
    # [1] FALSE  TRUE FALSE
    
    ## More rational, since R 3.4.0 :
    factor(c(1:2, NA), exclude =  "" ) # keeps <NA> , as
    factor(c(1:2, NA), exclude = NULL) # always did
    ## exclude = <character>
    z # ordered levels 'A < B < C'
    factor(z, exclude = "C") # does exclude
    factor(z, exclude = "B") # ditto
    
    ## Now, labels maybe duplicated:
    ## factor() with duplicated labels allowing to "merge levels"
    x <- c("Man", "Male", "Man", "Lady", "Female")
    ## Map from 4 different values to only two levels:
    (xf <- factor(x, levels = c("Male", "Man" , "Lady",   "Female"),
                     labels = c("Male", "Male", "Female", "Female")))
    #> [1] Male   Male   Male   Female Female
    #> Levels: Male Female
    
    ## Using addNA()
    Month <- airquality$Month
    table(addNA(Month))
    table(addNA(Month, ifany = TRUE))
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Ffactor&utm_source=r-docs&utm_medium=docs&utm_term=factor&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)