Replace filterD()

[This article was first published on fishR Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


We are deprecating filterD() from the next version of FSA (v0.9.0). It will likely be removed by the start of 2022. filterD() was an attempt to streamline the process of using filter() (from dplyr) followed by droplevels() to remove levels of a factor variable that no longer existed in the filtered data frame.

For example, consider the very simple data frame below.

d <- data.frame(tl=runif(6,min=100,max=200),
                spec=factor(c("LMB","LMB","SMB","BG","BG","BG")))
d
##         tl spec
## 1 120.8437  LMB
## 2 112.9690  LMB
## 3 126.4658  SMB
## 4 118.9474   BG
## 5 140.0974   BG
## 6 135.2961   BG

Now suppose that this data frame is reduced to just Bluegill.

dbg <- dplyr::filter(d,spec=="BG")

A quick frequency table of species caught shows that levels for species that no longer exist in the data frame are maintained.

xtabs(~spec,data=dbg)
## spec
##  BG LMB SMB 
##   3   0   0

This same “problem” occurs when using subset() from base R.

dbg <- subset(d,spec=="BG")
xtabs(~spec,data=dbg)
## spec
##  BG LMB SMB 
##   3   0   0

These “problems” can be eliminated by submitting the new data frame to drop.levels().

dbg2 <- droplevels(dbg)
xtabs(~spec,data=dbg2)
## spec
## BG 
##  3

filterD() was a simple work-around that eliminated this second step and was useful for helping students who were just getting started with R.

dbg3 <- FSA::filterD(d,spec=="BG")
xtabs(~spec,data=dbg3)
## spec
## BG 
##  3

However, this is a hacky solution to a simple problem. Thus, we are deprecating filterD() from FSA with plans to remove it by the beginning of next year. Thus, please use droplevels() (or fct_drop() from forcats) after using filter() to accomplish the same task of the soon to be defunct filterD().

To leave a comment for the author, please follow the link and comment on their blog: fishR Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)