| attr_factor | R Documentation |
This 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
attr_factor(data, verbose = TRUE, largestfirst = FALSE)
data |
the data.frame for which factors should be created |
verbose |
a logical indicating if datachecks should be printed to console |
largestfirst |
either a logical or a character vector indicating if the largest group should be the first level (see 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')
data frame with the formats assigned
Richard Hooijmaijers
factor, attr_add, attr_xls
dfrm <- data.frame(GENDER=rep(c(0,1),4),RESULT=rnorm(8))
attr(dfrm$GENDER,'format') <- c('0' = 'Male', '1' = 'Female')
dfrm <- attr_factor(dfrm)
str(dfrm$GENDER)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.