Learn R Programming

dunlin (version 0.1.10)

reformat: Reformat Values

Description

Reformat Values

Usage

reformat(obj, ...)

# S3 method for default reformat(obj, format, ...)

# S3 method for character reformat(obj, format, ..., verbose = FALSE)

# S3 method for factor reformat(obj, format, ..., verbose = FALSE)

# S3 method for list reformat( obj, format, ..., verbose = get_arg("dunlin.reformat.verbose", "R_DUNLIN_REFORMAT_VERBOSE", FALSE) )

Value

(character, factor or list of data.frame) with remapped values.

Arguments

obj

(character, factor or list of data.frame) to reformat.

...

for compatibility between methods and pass additional special mapping to transform rules.

  • .string_as_fct (flag) whether the reformatted character object should be converted to factor.

  • .to_NA (character) values that should be converted to NA. For factor, the corresponding levels are dropped. If NULL, the argument will be taken from the to_NAattribute of the rule.

  • .drop (flag) whether to drop empty levels. If NULL, the argument will be taken from the dropattribute of the rule.

  • .na_last (flag) whether the level replacing NA should be last.

format

(rule) or (list) of rule depending on the class of obj.

verbose

(flag) whether to print the format.

Examples

Run this code
# Reformatting of character.
obj <- c("a", "b", "x", NA, "")
attr(obj, "label") <- "my label"
format <- rule("A" = "a", "NN" = NA)

reformat(obj, format)
reformat(obj, format, .string_as_fct = FALSE, .to_NA = NULL)

# Reformatting of factor.
obj <- factor(c("first", "a", "aa", "b", "x", NA), levels = c("first", "x", "b", "aa", "a", "z"))
attr(obj, "label") <- "my label"
format <- rule("A" = c("a", "aa"), "NN" = c(NA, "x"), "Not_present" = "z", "Not_a_level" = "P")

reformat(obj, format)
reformat(obj, format, .na_last = FALSE, .to_NA = "b", .drop = FALSE)

# Reformatting of list of data.frame.
df1 <- data.frame(
  var1 = c("a", "b", NA),
  var2 = factor(c("F1", "F2", NA))
)

df2 <- data.frame(
  var1 = c("x", NA, "y"),
  var2 = factor(c("F11", NA, "F22"))
)

db <- list(df1 = df1, df2 = df2)

format <- list(
  df1 = list(
    var1 = rule("X" = "x", "N" = NA, .to_NA = "b")
  ),
  df2 = list(
    var2 = rule("f11" = "F11", "NN" = NA)
  ),
  df_absent = list(
    var1 = rule("NO" = "no")
  ),
  all_datasets = list(
    var1 = rule("xx" = "x", "aa" = "a")
  )
)

reformat(db, format)

Run the code above in your browser using DataLab