recodes: Recode one or more variables

Description Usage Arguments Details Value Note Examples

View source: R/recodes.R

Description

recodes recodes the values of one or more variables in a data frame

Usage

1
recodes(data, vars, from, to)

Arguments

data

a data frame.

vars

character vector of variable names.

from

a vector of values or conditions (see Details).

to

a vector of replacement values.

Details

Value

a data frame

Note

See the vignette for a worked example.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## Not run: 
# For variables A, B and C, convert 86, 99, and 999 to missing
df <- recodes(df, vars=c("A", "B", "C"), from=c(86, 99, 999), to=NA)

# For variables A, B, and C convert NA to 0
df <- recodes(df, vars=c("A", "B", "C"), from=NA, to=0)

# For variable X1, X2, and X3 convert 1 to Yes and 2 to No
df <- recodes(df, vars=c("X1", "X2", "X3"),
              from=c(1, 2), to=c("Yes", "No"))

# For variable SEX convert m to Male and f to Female
df <- recodes(df, vars="SEX",
              from=c("m", "f"), to=c("Male", "Female"))

# For variable OUTCOME convert Live to 0 and Die to 1
df <- recodes(df, vars="OUTCOME",
              from=c("live", "die"), to=c(0, 1))

# For variable AGE convert age <= 20 to young,
#    20 < age < =60 to middle, and age > 60 to old
df <- recodes(df, vars="AGE",
              from=c("$ < = 20", "$ > 20 & $ <= 60", "$  > 60"),
              to=c("young", "middle", "old"))

# Reverse code variable Rating from 1 to 10, to 10 to 1
df <- recodes(df, vars="Rating", from=1:10, to=10:1)


## End(Not run)

Rkabacoff/qacr documentation built on March 20, 2021, 3:03 p.m.