Apply Levels and Labels to Categorical Columns in a Data Frame
convertCatColsToFactors_IQRdataGENERAL.Rd
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
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.
See also
Other IQRdataGENERAL:
addCatColumnAsText_IQRdataGENERAL()
,
plotIndivAE_IQRdataGENERAL()
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))
} # }