filterD: Subsets/filters a data frame and drops the unused levels.

Description Usage Arguments Details Value IFAR Chapter Author(s) See Also Examples

View source: R/misc-utils.R

Description

Subsets/filters a data frame and drops the unused levels.

Usage

1
filterD(x, ..., except = NULL)

Arguments

x

A data frame.

...

further arguments to be passed to filter.

except

Indices of columns from which NOT to drop levels.

Details

Newbie students using R expect that when a factor variable is filtered with filter that any original levels that are no longer used after the filtering will be ignored. This, however, is not the case and often results in tables with empty cells and figures with empty bars. One remedy is to use droplevels immediately following filter. This generally becomes a repetitive sequence for most newbie students; thus, filterD incorporate these two functions into one function.

filterD is a wrapper for filter from dplyr followed by droplevels just before the data.frame is returned. Otherwise, there is no new code here.

This function is only used for data frames.

Value

A data frame with the filtered rows.

IFAR Chapter

Basic Data Manipulations.

Author(s)

Derek H. Ogle, derek@derekogle.com

See Also

See subset and filter from dplyr for similar functionality. See drop.levels in gdata and droplevels for related functionality.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
## The problem -- note use of unused level in the final table.
levels(iris$Species)
iris.set1 <- subset(iris,Species=="setosa" | Species=="versicolor")
levels(iris.set1$Species)
xtabs(~Species,data=iris)

## A fix using filterD
iris.set3 <- filterD(iris,Species=="setosa" | Species=="versicolor")
levels(iris.set3$Species)
xtabs(~Species,data=iris.set3)

droglenc/NCStats documentation built on June 5, 2021, 2:06 p.m.