
Last chance! 50% off unlimited learning
Sale ends in
recode.variables(data,recodes)
data.frame
to be recodeddata.frame
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")
cut
recode
in package 'car'
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