Apply Levels and Labels to Categorical Columns in a Data Frame

This function modifies the levels and labels of specified categorical columns in a data frame based on predefined category information stored as attributes.

convertCatColsToFactors_IQRdataGENERAL(
  x,
  catnames = attr(x, "catInfo")$COLNAME
)

Arguments

x

IQRdataGENERAL or IQRnlmeData object

catnames

(Optional) A character vector specifying the names of the categorical columns to be modified. If not provided, it defaults to the column names listed in the attributes.

Value

A modified version of the input data frame where specified categorical columns have their levels and labels updated according to the predefined category information.

Details

The function checks if the specified columns exist in the data frame and if they have the necessary attributes for category information (VALUES and VALUETXT). It then converts these columns into factors with appropriate levels and labels as defined by the values and texts provided.

Examples

if (FALSE) { # \dontrun{
df <- data.frame(SEX = c(0, 1, 1, 0), AGE = c(25, 30, 35, 40))
attr(df, "catInfo") <- data.frame(COLNAME = c("SEX"), NAME = c("Gender"), UNIT = c("-"), VALUETXT = c("Male,Female"), VALUES = c("0,1"))
attr(df, "class") <- c("IQRdataGENERAL", "data.frame")
df_with_levels <- convertCatColsToFactors_IQRdataGENERAL(df)
str(data.frame(df_with_levels))
} # }