Learn R Programming

mStats (version 3.2.2)

recode: Recode contents of a variable

Description

recode easily manipulates contents of a new variable of a data.frame

Usage

recode(data, var, old_values, new_values)

Arguments

data

dataset

var

name of a new variable

old_values

vector

new_values

vector (length of 1 or same with values_old)

>>>>

c(#, #) >>>> c(#, #)

c(#, #) >>>> #

#:# >>>> #

Details

recode changes the values of variables including categorical variables according to the rules specified below.

In case of factor, recode first converts the vector into character, recodes and then revert back to factor.

If data is specified, it returns the whole dataframe with recoded variables.

Sample Inputs for conversion:

Old.value to New.value

Examples

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

## tabulate induced to check values
tab(infert, induced)

## recode induced: 1 and 2 into 1
infert.new <- recode(infert, induced, c(1, 2), 1)

## tabulate to check
tab(infert.new, induced)

# }

Run the code above in your browser using DataLab