Scenario: After subset a data frame, the factor columns may include 0 frequncy levels
Related:
1. drop = TRUE, e.g. problem.factor <- problem.factor[, drop = TRUE]
2. drop.levels() function in gdata package
References:
http://r.789695.n4.nabble.com/Refactor-all-factors-in-a-data-frame-td826749.html
http://www.r-bloggers.com/data-types-part-3-factors/
http://rwiki.sciviews.org/doku.php?id=tips:data-manip:drop_unused_levels
Code:
refactor <- function (aDf){
cat <- sapply(aDf, is.factor);
aDf[cat] <- lapply(aDf[cat], factor);
}
No comments:
Post a Comment