recode_string: String recoding

Description Usage Arguments Details Value Note Examples

Description

This vectorized function codes (or maps) input vectors to strings. It is designed to take a list, or a named vector as the recode object.

Usage

1
2
3
recode_string(x, recode_key)

str_rec(x, recode_key)

Arguments

x

The input takes a vector of one to many strings, and is optimized to run without recursion using for loops or apply loops.

recode_key

A list of named character vectors or named character vectors. Adding 'else' into the named list will recode all unspecified values to the else value.

Details

This function uses data.table and an optimized version of melt from reshape2 (via data.table) in order to provide speedy string replacement. Primarily designed for use in data.table, but works on stand-alone vectors as well.

Value

A recoded character vector. If the input was class factor, the output will be converted to class character.

Note

1. str_rec is a mask for recode_string. 2. May actually be slower for small datasets, but speed does not matter in such cases. Refactoring is a feature request we will consider upon user request.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
x <- c("A", "E", 7, "I", "11", "fifteen", 16, NA)

# EXAMPLE 1
recode_key = list(
  '1' = c("A", "B", "C"),
  DD  = c("D", "E", 7),
  '2' = c("H", "I", "J"),
  last_num = c(11, 12, 13),
  'else' = c("somethingelse")
)
recode_string(x, recode_key)

# EXAMPLE 2
recode_key = c(
  "'1'  = c('A','B','C')",
  "'DD' = c('D','E', 7)",
  "'2'  = c('H','I','J')",
  "'last_num' = 11:13",
  "'else' = NA"
)
recode_string(x, recode_key)

# EXAMPLE 3
recode_key = c(
  "'1'  = c('A','B','C')"
)
recode_string(x, recode_key)

# EXAMPLE 4
recode_key = c(
  "'1'  = c('A','B','C');DD = c('D','E', 7);'2' = c('H','I','J');
  last_num = 11:13;'else' = NA"
)
recode_string(x, recode_key)

# EXAMPLE 5
recode_key = list(
  '1' = c("A", "B", "C"),
  DD  = c("D", "E",  7),
  '2' = c("H", "I", "J"),
  last_num = c(11, 12, 13),
  'else' = c(NA))
recode_string(x, recode_key)

## Not run: 
recode_key = c(
  "1  = c('A','B','C)",
  "DD = c('D','E', 7)",
  "2 = c('H','I','J')",
  "last_num = 11:13",
  "else = NA")

## End(Not run)

equastat/Emisc documentation built on May 16, 2019, 8:24 a.m.