replace_NA: Replaces NA

View source: R/replace_NA.R

replace_NAR Documentation

Replaces NA

Description

Replaces NA in characters, factors and data.frames.

Usage

replace_NA(x, ...)

## S3 method for class 'character'
replace_NA(x, replacement = atable_options("replace_NA_by"), ...)

## S3 method for class 'factor'
replace_NA(x, ...)

## S3 method for class 'ordered'
replace_NA(x, ...)

## S3 method for class 'data.frame'
replace_NA(x, ...)

## S3 method for class 'list'
replace_NA(x, ...)

## Default S3 method:
replace_NA(x, ...)

Arguments

x

An object.

...

Passed to methods.

replacement

A character of length 1. Default value is defined in atable_options('replace_NA_by'), see atable_options.

Details

The atable package aims to create readable tables. For non-computer-affine readers NA has no meaning. So replace_NA exists.

Methods for character, factor, ordered, list and data.frame available. Default method returns x unchanged.

Gives a warning when replacement is already present in x and does the replacement.

Silently returns x unchanged when there are no NA in x.

Silently returns x unchanged when replacement is not a character of length 1 or when replacement is NA.

Value

Same class as x, now with NA replaced by replacement.

Methods (by class)

  • replace_NA(character): replaces NA with replacement.

  • replace_NA(factor): applies replace_NA to the levels of the factor. A factor with length > 0 without levels will get the level replacement.

  • replace_NA(ordered): as factor.

  • replace_NA(data.frame): applies replace_NA to all columns.

  • replace_NA(list): applies replace_NA to all elements of the list.

  • replace_NA(default): return x unchanged.

Examples

Character <- c(NA,letters[1:3], NA)
Factor <- factor(Character)
Ordered <- ordered(Factor)
Numeric <- rep(1, length(Factor))
Factor_without_NA <- factor(letters[1:length(Factor)])

DD <- data.frame(Character, Factor, Ordered,
                Numeric, Factor_without_NA,
                stringsAsFactors = FALSE)
## Not run: 
DD2 <- replace_NA(DD, replacement = 'no value')

summary(DD)
summary(DD2) # now with 'no value' instead NA in column Character, Factor and Ordered

atable_options(replace_NA_by = 'not measured') # use atable_options to set replacement
DD3 <- replace_NA(DD)
summary(DD3) # now with 'not measured' instead NA

atable_options_reset() # set 'replace_NA_by' back to default

## End(Not run)

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