Learn R Programming

dentomedical (version 0.2.0)

recode_data: Recode values in a data frame using a lookup table

Description

This function replaces values in a data frame according to a named lookup vector. All columns are converted to character, and any value matching a name in lookup will be replaced by its corresponding value.

Usage

recode_data(data, lookup)

Value

A data frame with the same structure as data, with values recoded according to lookup.

Arguments

data

A data frame whose values you want to recode.

lookup

A named character vector where names are original values and elements are the new values.

Examples

Run this code
df <- data.frame(
  gender = c("male", "F", "male", "female"),
  status = c("single", "Married", "oo", "M"),
  stringsAsFactors = FALSE
)

lookup <- c(
  "male" = "Male",
  "M" = "Married",
  "oo" = "Widow",
  "female" = "Female",
  "F" = "Female"
)

df_recode <- recode_data(df, lookup)
print(df_recode)

Run the code above in your browser using DataLab