Displaying 1 result from an estimated 1 matches for "addmissinglevel".
2011 Jun 14
1
Expand DF with all levels of a variable
Dear list,
I would like to expand a DF with all the missing levels of a variable.
a <- c(2,2,3,4,5,6,7,8,9)
a.cut <- cut(a, breaks=c(0,2,6,9,12), right=FALSE )
(x <- data.frame(a, a.cut))
# In 'x' the level "[0,2)" is "missing".
AddMissingLevel <- function(xdf) {
xfac <- factor( c("[0,2)", "[2,6)", "[6,9)", "[9,12)") )
xlevels <- levels(xfac)
if(length(xlevels) != nlevels(factor(xdf$a.cut))) {
v <- setdiff(xlevels, factor(xdf$a.cut))
u <- data.frame(a...