Learn R Programming

sjmisc (version 2.8.10)

rec_pattern: Create recode pattern for 'rec' function

Description

Convenient function to create a recode pattern for the rec function, which recodes (numeric) vectors into smaller groups.

Usage

rec_pattern(from, to, width = 5, other = NULL)

Value

A list with two values:

pattern

string pattern that can be used as rec argument for the rec-function.

labels

the associated values labels that can be used with set_labels.

Arguments

from

Minimum value that should be recoded.

to

Maximum value that should be recoded.

width

Numeric, indicating the range of each group.

other

String token, indicating how to deal with all other values that have not been captured by the recode pattern. See 'Details' on the else-token in rec.

See Also

group_var for recoding variables into smaller groups, and group_labels to create the asssociated value labels.

Examples

Run this code
rp <- rec_pattern(1, 100)
rp

# sample data, inspect age of carers
data(efc)
table(efc$c160age, exclude = NULL)
table(rec(efc$c160age, rec = rp$pattern), exclude = NULL)

# recode carers age into groups of width 5
x <- rec(
  efc$c160age,
  rec = rp$pattern,
  val.labels = rp$labels
)
# watch result
frq(x)

Run the code above in your browser using DataLab