Learn R Programming

Deducer (version 0.7-9)

recode.variables: Recode

Description

Recodes a set of variables according to a set of rules

Usage

recode.variables(data,recodes)

Arguments

data
A data.frame to be recoded
recodes
Definition of the recoding rules. See details

Value

returns a recoded data.frame

Details

recodes contains a set of recoding rules separated by ";". There are three different types of recoding rules:

1. The simplest codes one value to another. If we wish to recode 1 into 2, we could use the rule "1->2;".

2. A range of values can be coded to a single value using "1:3->4;". This rule would code all values between 1 and 3 inclusive into 4. For factors, a value is between two levels if it is between them in the factor ordering. One sided ranges can be specified using the Lo and Hi key words (e.g."Lo:3->0; 4:Hi->1")

3. Default conditions can be coded using "else." For example, if we wish to recode all values >=0 to 1 and all values <0 to="" missing,="" we="" could="" use="" ("0:Hi->1; else->NA")

See Also

cut recode in package 'car'

Examples

Run this code
data<-data.frame(a=rnorm(100),b=rnorm(100),male=rnorm(100)>0)
recode.variables(data[c("a","b")] , "Lo:0 -> 0;0:Hi -> 1;")
data[c("male")] <- recode.variables(data[c("male")] , "1 -> 'Male';0 -> 'Female';else -> NA;")

Run the code above in your browser using DataLab