R: Transform missing values of a factor to an extra level

addNAstr {questionr} | R Documentation

## Transform missing values of a factor to an extra level

### Description

This function modifies a factor by turning `NA` into an extra level (so that `NA` values are counted in tables, for instance). This version of `addNA` extends the same function provided in R by allowing to specify a string name for the extra level (see examples).

### Usage

    addNAstr(x, value = "NA", ...)

### Arguments

`x` | a vector of data, usually taking a small number of distinct values.
`value` | string to use for the extra level name. If NULL, the extra level is created as NA, and the result is the same as the one of the `addNA` function.
`...` | arguments passed to `addNA`.

### Value

an object of class `"factor"`, original missing values being coded as an extra level named `NA` if `as.string=FALSE`, `"NA"` if `as.string=TRUE`, as specified by `as.string` if `as.string` is a string.

### Source

Adapted from James (http://stackoverflow.com/a/5817181) by Joseph Larmarange <joseph@larmarange.net>

### See Also

`addNA` (base).

### Examples

    f <- as.factor(c("a","b",NA,"a","b"))
    f
    addNAstr(f)
    addNAstr(f, value="missing")
    addNAstr(f, value=NULL)

[Package questionr version 0.6.3 Index]