Labels and metadata
Start with the expanded example for each command. Open the variants for options and alternative R approaches. Read the qualifications before adapting the code.
char
Inspect custom column characteristics
Stata
char list x[]R Package:base
attributes(data$x)Returns all R attributes, including any custom metadata.
Store a custom column characteristic
Stata
char x[source] "questionnaire"R Package:base
attr(data$x,'source') <- 'questionnaire'R attributes serve as local metadata; many transformations do not preserve arbitrary attributes.
decode
Decode numeric value labels to text
Stata
decode x, generate(text)R Package:haven
data$text <- as.character(haven::as_factor(data$x))encode
Encode nonempty categories in sorted order
Stata
encode s, generate(code)R Package:base
data$code <- as.integer(factor(data$s,levels=sort(unique(data$s[!is.na(data$s)&data$s!='']))))Differs from Stata Creates integer codes only. Add a labelled vector or keep the factor to retain category labels; collation is locale-dependent.
label
Attach a dataset label
Stata
label data "Research sample"R Package:base
attr(data,'label') <- 'Research sample'Fixed workflow example; adapt the object and column names together.
6 more examples and alternatives
Define a reusable value-label mapping
Stata
label define yesno 1 "No" 2 "Yes"R Package:base
yesno <- c(No=1,Yes=2)R stores the mapping as a named vector; attach it separately to each labelled column.
Read a column’s value-label mapping
Stata
label list yesnoR Package:base
attr(data$x,'labels')Fixed workflow example; adapt the object and column names together.
Remove value labels while preserving numeric values
Stata
label values xR Package:haven
data$x <- haven::zap_labels(data$x)Attach a numeric value-label dictionary
Stata
label values x yesnoR Package:haven
data$x <- haven::labelled(data$x,labels=yesno)Remove a variable label
Stata
label variable x ""R Package:base
attr(data$x,'label') <- NULLAttach a descriptive variable label
Stata
label variable x "Response"R Package:base
attr(data$x,'label') <- 'Response'labelbook
Inspect value-label dictionaries by column
Stata
labelbookR Package:base
lapply(data,function(z) attr(z,'labels'))Differs from Stata Lists attached dictionaries; does not reproduce Stata’s full multilingual label audit.
labmask
Assign existing text values as numeric value labels
Stata
labmask code, values(text)R Package:haven
dictionary <- unique(data[c('code','text')]); stopifnot(!anyDuplicated(dictionary$code)); data$code <- haven::labelled(data$code,stats::setNames(dictionary$code,dictionary$text))Differs from Stata Requires a unique text label per observed numeric code; this preserves supplied numeric codes rather than alphabetically encoding text.
notes
Read notes attached to a column
Stata
notes list xR Package:base
attr(data$x,'notes')Reads a custom R notes attribute; preservation depends on subsequent transformations.
1 more examples and alternatives
Attach a text note to a column
Stata
notes x: Measured at baselineR Package:base
attr(data$x,'notes') <- 'Measured at baseline'R attribute workflow; numbered Stata note management is not recreated.
numlabel
Prefix each label with its numeric code
Stata
numlabel, addR Package:base
labs <- attr(data$x,'labels'); names(labs) <- paste0(unname(labs),'. ',names(labs)); attr(data$x,'labels') <- labsFixed workflow example; adapt the object and column names together.
Remove a known numeric-code label prefix
Stata
numlabel, removeR Package:base
labs <- attr(data$x,'labels'); names(labs) <- sub('^[0-9]+[.] ','',names(labs)); attr(data$x,'labels') <- labsDiffers from Stata Targets the demonstrated positive-integer prefix format; other masks require a matching pattern.
uselabel
Make a value-label dictionary table
Stata
uselabel using sample.dta, clearR Package:base
labs <- attr(data$x,'labels'); dictionary <- data.frame(lname='x',value=unname(labs),label=names(labs),trunc=0L)Uses an already imported labelled column. R has no separate Stata label-definition name; lname uses the column name.