R: Recode
recode.variables R Documentation
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
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"
)
Value
returns a recoded data.frame
Author(s)
Ian Fellows adapted from code by John Fox
See Also
cut
recode
in package 'car'
Examples
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;")
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(Deducer)
Loading required package: ggplot2
Loading required package: JGR
Loading required package: rJava
Loading required package: JavaGD
Loading required package: iplots
Please type JGR() to launch console. Platform specific launchers (.exe and .app) can also be obtained at http://www.rforge.net/JGR/files/.
Loading required package: car
Loading required package: MASS
Note Non-JGR console detected:
Deducer is best used from within JGR (http://jgr.markushelbig.org/).
To Bring up GUI dialogs, type deducer().
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Deducer/recode.variables.Rd_%03d_medium.png", width=480, height=480)
> ### Name: recode.variables
> ### Title: Recode
> ### Aliases: recode.variables
>
> ### ** Examples
>
> 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;")
recode.other.variable. recode.other.variable..1
1 0 1
2 0 1
3 0 0
4 1 0
5 1 0
6 1 1
7 1 1
8 0 1
9 1 1
10 1 1
11 1 1
12 1 0
13 0 0
14 1 0
15 1 1
16 0 0
17 1 0
18 1 1
19 0 0
20 0 1
21 0 1
22 1 1
23 0 1
24 0 0
25 0 0
26 0 1
27 1 1
28 1 0
29 1 1
30 1 0
31 1 0
32 1 1
33 1 1
34 0 0
35 0 1
36 0 0
37 0 1
38 0 0
39 1 1
40 0 0
41 0 1
42 0 0
43 0 1
44 0 1
45 1 0
46 1 1
47 1 0
48 1 1
49 0 0
50 0 1
51 1 0
52 0 1
53 0 1
54 1 1
55 1 1
56 1 0
57 1 0
58 0 0
59 1 1
60 1 0
61 0 1
62 1 1
63 0 0
64 0 0
65 0 1
66 1 1
67 0 0
68 0 1
69 1 0
70 1 1
71 0 1
72 1 0
73 1 0
74 0 1
75 0 0
76 1 0
77 0 0
78 1 1
79 1 0
80 1 1
81 0 0
82 1 0
83 0 0
84 1 0
85 1 0
86 1 0
87 1 0
88 1 1
89 1 1
90 0 0
91 0 0
92 1 1
93 1 1
94 0 0
95 0 1
96 0 1
97 0 1
98 1 0
99 0 0
100 0 1
> data[c("male")] <- recode.variables(data[c("male")] , "1 -> 'Male';0 -> 'Female';else -> NA;")
>
>
>
>
>
> dev.off()
null device
1
>