Online training class for Clinical R programming batch starts on Monday, 02Feb2026.
Click here for details.
library(tidyverse)
# Sample tibble with coded values
dm <- tibble( usubjid = c("01", "02", "03", "04"), sex = c(1, 2, 1, 2) )
# Define named vector format
sex_fmt <- c("1" = "Male", "2" = "Female")
# Apply the format using mutate
dm_labeled <- dm %>%
mutate( sexc = sex_fmt[as.character(sex)] ) dm <- data.frame(
usubjid = c("01", "02", "03", "04"),
sex = c(1, 2, 1, 2)
, stringsAsFactors = FALSE
)
sex_fmt <- c("1" = "Male", "2" = "Female")
dm_labeled <- dm
dm_labeled$sexc <- sex_fmt[as.character(dm_labeled$sex)]