Learn R Programming

mStats (version 3.2.2)

replace: Change contents of an existing variable

Description

replace() alters the values of a variable when specified conditions are met.

replace(data, var, value,
     var < somevalue | var > somevalue, is.na(var))

If conditions are not specified, replac() changes the whole variable with specified value.

replace(data, var, value)

Usage

replace(data, var, value, ...)

Arguments

data

Dataset

var

Variable

value

Replacement value

...

if conditions.

Value

Modified Dataset

Details

It is used when multiple conditions have to be met to change a value. The function first checks whether specified value is a variable of the dataset. If yes, then the values are replaced with those of that variables with the conditions.

Examples

Run this code
# NOT RUN {
## use infert dataset
data(infert)

## replace parity == NA if parity > 4
tab(infert, parity)

infert.new <- replace(infert, parity, NA, parity > 4)
tab(infert.new, parity)

## replace education as character
infert.new <- replace(infert, education, as.character(education))
codebook(infert.new)
tab(infert.new, education)

# }

Run the code above in your browser using DataLab