recode_errors: Recode a vector of erroneous values in a vector or data frame...

View source: R/miscellaneous.R

recode_errorsR Documentation

Recode a vector of erroneous values in a vector or data frame column(s).

Description

Recode a vector of erroneous values for a vector or column(s) in a dataframe by replacing those values with NA or another common indicator value. Similar to na_if but is more flexible, e.g. it can operate on multiple columns/rows (for data frames), or value indices (for vectors).

Usage

recode_errors(
  data,
  errors,
  replacement = NA,
  rows = NULL,
  cols = NULL,
  ind = NULL
)

Arguments

data

A vector, data frame, tibble, or matrix.

errors

A vector of erroneous values to be recoded.

replacement

The value you wish to replace all errors with. Default = NA.

rows

A vector specifying the rows of the data object for which to replace erroneous values. Default is all rows in data. Use "ind" instead if data is a vector.

cols

A vector specifying the columns of the data object for which to replace erroneous values. Default is all columns in data. Use "ind" instead if data is a vector.

ind

If data is a vector, this accepts a vector specifying the indices the data object for which to replace erroneous values. Default is all indices in data. Use "rows" &/or "cols" to specify indices to operate upon if data is not a vector.

Author(s)

Craig P. Hutton, Craig.Hutton@gov.bc.ca

Examples


#if (hypothetically) values of 0 & 8 were actually data entry errors in the
#mtcars dataset these can easily be recoded as NAs

data(mtcars)

recode_errors(mtcars,
  cols = c(8:11), #specify a column number range
  errors = c(0, 8))


bcgov/elucidate documentation built on Sept. 3, 2022, 7:16 p.m.