
Create factors for variables within a data frame using the format attribute
Source:R/attr_factor.r
attr_factor.RdThis function searches for the 'format' attribute within a data frame, if found it applies the format to that variable. The resulting variable will be a factor useful for plotting and reporting
Details
In order to make this function work the 'format' attribute should be present and should be available
as a named vector (e.g. attr(data$GENDER,'format') <- c('0' = 'Male', '1' = 'Female')). If the
attribute is found it overwrites the variable with the format applied to it. Be aware that the original
levels defined in the format could be lost in this process.
The 'largestfirst' argument could be set to a logical which indicates if a for all variables in the dataset, the
largest group should be the first level. The argument could also be a character vector indicating for which of the variables
in the dataset, the largest group should be the first level. In case you want to set a specific order, this can be done
directly in the the format attribute, e.g. attr(data$VAR,'format') <- c('2' = 'level 1', '1' = 'Level 2')