recode_conditional: Conditionally recode values within a data frame column based...

Description Usage Arguments Value Examples

View source: R/recode_conditional.R

Description

Conditionally recode values within a data frame column based on one or more dictionaries containing columns to match against, and corresponding replacement values to insert where a match is made

Usage

1
recode_conditional(df, dict, col_recode, flag_recoded = FALSE)

Arguments

df

A data.frame-like object

dict

The dictionary. A data frame, or list of data frames, each containing a column of replacement values, and one or more columns to match against df (matching via dplyr::left_join).

col_recode

Name (character) of the column to be recoded. This column must be present both within df and dict.

flag_recoded

Logical indicating whether to add a column to the output indicating whether the given row has been matched by a dictionary entry and therefore recoded. Defaults to FALSE. If TRUE, the new column will be called <col_recode>_is_recoded.

Value

A data.frame corresponding to df, but with values in column col_recode recoded based on the given dictionary.

If flag_recoded is TRUE, the output will contain an additional column <col_recode>_is_recoded, indicating whether the given row has been matched by a dictionary entry and therefore recoded.

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
# dictionaries
dict1 <- data.frame(
  x1 = c("a", "b", "c", "d"),
  y = c("alpha", "bravo", "charlie", "delta"),
  stringsAsFactors = FALSE
)

dict2 <- data.frame(
  x1 = c("0", "1", "2", "3"),
  y = c("zero", "one", "two", "three"),
  stringsAsFactors = FALSE
)

# data frame with column to be recoded ('y')
dat <- data.frame(
  x1 = c("a", "c", "c", "d", "b", "0"),
  y = c("???", "???", "???", "???", "???", "???"),
  stringsAsFactors = FALSE
)

# recode column "y" based on dictionary 'dict1'
recode_conditional(dat, dict = dict1, col_recode = "y")

# recode column "y" based on dictionaries 'dict1' and 'dict2'
recode_conditional(dat, dict = list(dict1, dict2), col_recode = "y")

epicentre-msf/llutils documentation built on Nov. 9, 2020, 8:24 p.m.