recode_string: String recoding

Description Usage Arguments Details Value TODO Note Examples

Description

This function is an optimized version of recode. It is designed to take a list, or a named vector as the recode object.

Usage

1
2
3
4
5
recode_s(x, recode_key)

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.

TODO

Note

May actually be slower for small datasets, but speed does not matter in such cases. Refactoring could be an option if users report a desire for such an option.

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
x <- c("A", "E", 7, "I", "11", "fifteen", 16)
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)
recode_key = c(
  "'1'  = c('A','B','C')"
)
recode_string(x, recode_key)
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)
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)
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)

JamesDalrymple/wccmh documentation built on May 7, 2019, 10:20 a.m.