merges levels of selected variables in a data frame either according to minimum numbers in a category or according to user-defined rules.
mergelevels.syn(data, vars = NULL, newlabel = FALSE, addNA = FALSE,
print.flag = FALSE, minsize = 10, merge.byhand =
FALSE, merge.details = NULL)
A data frame of the same size and structure as data
with levels of selected variables merged.
An observed data set before synthesis.
a vector of names or numbers for the variables for which categories are to be merged. defaults to all factors in data when sert to NULL
The minimum size that of combined categories when merge.byhand
is FALSE
When merge.byhand is FALSE when newlabel = TRUE
causes the new factor
levels to be labelled with a concatenation of the labels of all the categories that have
contributed. newlabel = FALSE
labels the new category "Other"
Causes the NA category to be included when determining which groups are below
minsize
and also to allow NA values, designated by "NA", to be combined with others
in merge.details
prints tables of variables before and after recoding.
Uses the information in merge.byhand
to create groupings, ignoring
the parameters minsize
and vars
.
A named list of variable names with names giving the names of the variables that will have levels merged. Each item is a vector where the first item is the name for the new combined level and the other entries are the levels to be merged. If it exists already the levels with small counts will be added to it, otehrwise a new level will be formed.
sdc
test <- SD2011[1:20]
data.mlevs1 <- mergelevels.syn(test, vars = c(3,5,18:20),minsize = 20, addNA = TRUE,
print.flag = TRUE, newlabel = TRUE)
mlevs <- list(agegr = c("60+", "60-64" , "65+"),socprof = c("NEW","UNEMPLOYED","FARMER"))
data.mlevs2 <-mergelevels.syn(test, merge.byhand = TRUE, merge.details = mlevs,
addNA=TRUE, print.flag = TRUE)
Run the code above in your browser using DataLab