recode_valinvec {trustmebro}R Documentation

Recode a variable

Description

Recode a specified variable in a data frame or tibble based on a provided recode map. If the recode map is empty, the original variable is retained under a new name.

Usage

recode_valinvec(data, var, recode_map, new_var)

Arguments

data

A data frame or tibble.

var

A variable to be recoded.

recode_map

A named vector specifying the recode map.

new_var

Name of the new variable holding the recoded values.

Value

A data frame or tibble with the new variable added.

Examples

# Example data
print(sailor_students)

# Define a recode map for gender
recode_map_gender <- c("Female" = "F", "Male" = "M", "Other" = "X")

# Recode gender
sailor_students_recoded <- 
recode_valinvec(sailor_students, gender, recode_map_gender, recode_gender)

# A tibble with a recoded gender variable
print(sailor_students_recoded)

[Package trustmebro version 1.0.0 Index]