Learn R Programming

kutils (version 1.73)

n2NA: Convert nothing to R missing(NA).

Description

By "nothing", we mean white space or other indications of nothingness. Goal is to find character strings that users might insert in a key to indicate missing values. Those things, which are given default values in the argument nothings, will be changed to NA.

Usage

n2NA(x, nothings = c("\\.", "\\s"), zapspace = TRUE)

Value

A vector with "nothing" values replaced by R's NA symbol. Does not alter other values in the vector. Previous version had applied zapspace to non-missing values, but it no longer does so.

Arguments

x

A character vector. If x is not a character vector, it is returned unaltered without warning.

nothings

A vector of values to be matched by regular expressions as missing. The default vector is c("\.", "\s"), where "\." means a literal period (backslashes needed to escape the symbol which would otherwise match anything in a regular expression).

zapspace

Should leading and trailing white space be ignored, so that, for example " . " and "." are both treated as missing.

Author

Paul Johnson <pauljohn@ku.edu>

Details

Using regular expression matching, any value that has nothing except for the indicated "nothing" values is converted to NA. The "nothing" values included by default are a period by itself (A SAS missing value), an empty string, or white space, meaning " ", or any number of spaces, or a tab.

Examples

Run this code
gg <- c("", " ", "   ", "\t", "\t some", "some\t", " space first", ".",
       " . ")
n2NA(x = gg)
n2NA(x = gg, zapspace = FALSE)
n2NA(x = gg, nothings = c("\\s"), zapspace = FALSE)
n2NA(x = gg, nothings = c("\\."), zapspace = TRUE)
n2NA(x = gg, nothings = c("\\."), zapspace = FALSE)

Run the code above in your browser using DataLab