From 4ec3f31847f64a88b5786aee878e3ab75ac22d23 Mon Sep 17 00:00:00 2001 From: Fernando Mayer <fernandomayer@gmail.com> Date: Wed, 12 Aug 2015 17:33:51 -0300 Subject: [PATCH] first function --- R/subset.drop.all.R | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 R/subset.drop.all.R diff --git a/R/subset.drop.all.R b/R/subset.drop.all.R new file mode 100644 index 0000000..d31162e --- /dev/null +++ b/R/subset.drop.all.R @@ -0,0 +1,33 @@ +##' @title Drop unused levels after subset +##' @name subset.drop.all +##' +##' @description This function is designed to be used in place of +##' \code{\link[base]{subset}} when you want to drop off all empty +##' levels, of all factor columns in a data frame. +##' +##' @param x Object to be subsetted +##' @param ... further arguments to be passed to or from other methods +##' +##' @details This function works exactly the same way as +##' \code{\link[base]{subset}} but it will drop off all empty levels +##' of all columns that are factors in \code{x}. +##' +##' @return An object similar to \code{x} containing just the selected +##' rows and columns (for a data frame). Column factors that have +##' one or more levels emptied after the subset, will have this +##' levels droped off. +##' +##' @author Fernando Mayer \email{fernando.mayer@ufpr.br} +##' +##' @seealso \code{\link[base]{subset}} +##' +##' @export +subset.drop.all <- function(x, ...){ + if(!is.data.frame(x)) stop("'x' must be a data.frame") + x <- subset(x, ...) + factors <- which(sapply(x, class) %in% "factor") + for(i in factors){ + x[,i] <- sapply(x[,i], "[", drop=T) + } + return(x) +} -- GitLab