Replace filterD()

An alternative to filterD() which was removed from FSA.
FSA
Data Wrangling
Author

Derek H. Ogle

Published

May 26, 2021

Modified

Dec 19, 2022

Note

The following packages are loaded for use below.

library(dplyr)  ## for filter()
Warning

Some functions illustrated below were in the FSA package but have now been removed and put into the non-released FSAmisc package that I maintain. These functions are used below only to show what could be done in older versions of FSA but should now be done as described in this post. DO NOT USE any of the functions below that begin with FSAmisc::.

 

We deprecated filterD() from FSA v0.9.0 and fully removed it 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
#R|          tl spec
#R|  1 157.5134  LMB
#R|  2 185.0328  LMB
#R|  3 150.1690  SMB
#R|  4 178.2323   BG
#R|  5 163.9159   BG
#R|  6 174.6475   BG

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

dbg <- d |>
  filter(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)
#R|  spec
#R|   BG LMB SMB 
#R|    3   0   0

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

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

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

dbg2 <- droplevels(dbg)
xtabs(~spec,data=dbg2)
#R|  spec
#R|  BG 
#R|   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 <- FSAmisc::filterD(d,spec=="BG")
xtabs(~spec,data=dbg3)
#R|  spec
#R|  BG 
#R|   3

However, this was a hacky solution to a simple problem. Thus, we deprecated and subsequently removed filterD() from FSA. Thus, please use droplevels() (or fct_drop() from forcats) after using filter() to accomplish the same task of the defunct filterD().

 

Reuse

Citation

BibTeX citation:
@online{h. ogle2021,
  author = {H. Ogle, Derek},
  title = {Replace {filterD()}},
  date = {2021-05-26},
  url = {https://fishr-core-team.github.io/fishR//blog/posts/2021-5-26_filterD-replacement},
  langid = {en}
}
For attribution, please cite this work as:
H. Ogle, D. 2021, May 26. Replace filterD(). https://fishr-core-team.github.io/fishR//blog/posts/2021-5-26_filterD-replacement.