n2NA: Convert nothing to R missing(NA).

View source: R/variableKey.R

n2NAR Documentation

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)

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.

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.

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.

Author(s)

Paul Johnson <pauljohn@ku.edu>

Examples

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)

kutils documentation built on Sept. 17, 2023, 5:06 p.m.