Reformatting

Introduction

Reformatting in dunlin consists in replacing predetermined values by another in particular variables for selected tables of a data set stored.

This is performed in two steps:

  1. A Reformatting Map (rule object) is created which specifies the correspondence between the old and the new values

  2. The reformatting itself is performed with the dunlin::reformat() function.

The Formatting Map Structure

The Reformatting Map is a rule object inheriting from character. Its names are the new values to be used, and its values are the old values to be used.

rule(A = "a", B = c("c", "d"))
#> Mapping of:
#> A  <-  a 
#> B  <-  c 
#> B  <-  d 
#> NA <-   
#> Convert to factor: TRUE 
#> Drop unused level: FALSE 
#> NA-replacing level in last position: TRUE

This rule will replace “a” with “A”, replace “c” or “d” with “B”.

Calling reformat

reformat is a generic supports reformatting of character or factor. Reformatting for other types of variables is meaningless. reformat will also preserve the attributes of the original data, e.g. the data type or labels will be unchanged.

An example of reformatting character can be

r <- rule(A = "a", B = c("c", "d"))
reformat(c("a", "c", "d", NA), r)
#> [1] A    B    B    <NA>
#> Levels: A B

We can see that the NA values are not changed.

Now we test the factor reformatting:

r <- rule(A = "a", B = c("c", "d"))
reformat(factor(c("a", "c", "d", NA)), r)
#> [1] A    B    B    <NA>
#> Levels: A B

The NA values are also not changed. However, if we including reformatting for the NA, there is something different:

r <- rule(A = "a", C = NA, B = c("c", "d"))
reformat(factor(c("a", "c", "d", NA)), r)
#> [1] A B B C
#> Levels: A B C

By default, the level replacing NA is set as the last one. This can be changed by setting .na_last = FALSE.

r <- rule(A = "a", C = NA, B = c("c", "d"))
reformat(factor(c("a", "c", "d", NA)), r, .na_last = FALSE)
#> [1] A B B C
#> Levels: A C B

For list of data.frames, the format argument is actually a nested list of rule. The first layer indicates the table names, the second layer indicates the variables in that table. Reformatting is only available for columns of characters or factors, reformatting columns of another types will result in a warning.

Example

Rule Attributes

The behavior of a rule can be further refined using special mapping values. * .to_NA convert the specified character to NA at the end of the process.

r <- rule(A = "a", B = c("c", "d"), .to_NA = c("x"))
reformat(c("a", "c", "d", NA, "x"), r)
#> [1] A    B    B    <NA> <NA>
#> Levels: A B
# With drop = FALSE
obj <- factor(c("a", "c", "d", NA), levels = c("d", "c", "a", "Not used"))
r <- rule(A = "a", B = c("c", "d"))
reformat(obj, r)
#> [1] A    B    B    <NA>
#> Levels: A B Not used

# With drop = TRUE
obj <- factor(c("a", "c", "d", NA), levels = c("d", "c", "a", "Not used"))
r <- rule(A = "a", B = c("c", "d"), .drop = TRUE)
reformat(obj, r)
#> [1] A    B    B    <NA>
#> Levels: A B

Note that behavior of the rule can be overridden using the corresponding arguments in reformat.

r <- rule(A = "a", B = c("c", "d"), .to_NA = c("x"), .drop = TRUE)
obj <- factor(c("a", "c", "d", NA, "x", "y"), levels = c("d", "c", "a", "Not used", "x", "y"))

reformat(obj, r)
#> [1] A    B    B    <NA> <NA> y   
#> Levels: A B y

# Override
reformat(obj, r, .to_NA = "y", .drop = FALSE)
#> [1] A    B    B    <NA> x    <NA>
#> Levels: A B Not used x