recode: Recode variables

Description Usage Arguments Details Examples

Description

Recode variables

Usage

1
recode(x, ..., default = NULL, coerce = NULL)

Arguments

x

numeric or character vector with values to recode.

...

recode pairs in the format "from" = "to", e.g. "female" = "f".

default

numeric or character. All entries in x not covered by ... get recoded to default.

coerce

a function that is applied to x after recoding, e.g. as.numeric.

Details

The left hand side of the recode pairs in ... can be one of:

Single value

e.g. "CTX" = "Context", "3" = 5

Range of values

e.g. "1:10" = 5, "6:10" = "above five"

Vector of values

e.g. "c('A', 'B')" = "AB" (note the use of single quotes inside the double quotes)

Logical operation

e.g. "< 20" = "small", "<= 10 | > 90" = "outlier", "> 40 & < 60" = "normal"

Examples

1
2
3
4
5
6
7
8
9
recode(c("female", "male", "male", "female"), "female" = "f", "male" = "m")

recode(1:10, "1:5" = "a", "6:10" = "b")
recode(1:20, "1:10" = 5, "11:20" = 15)

recode(1:20, "< 3" = "small", ">= 3 & <= 8" = "medium", "> 8" = "large")

recode(sample(letters[1:6]), "c('a', 'b', 'c')" = "abc",
       "c('d', 'e', 'f')" = "def")

dgromer/psymisc documentation built on May 15, 2019, 7:22 a.m.