library(dplyr) ## for filter()
The following packages are loaded for use below.
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.
<- data.frame(tl=runif(6,min=100,max=200),
d 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.
<- d |>
dbg 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.
<- subset(d,spec=="BG")
dbg 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()
.
<- droplevels(dbg)
dbg2 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.
<- FSAmisc::filterD(d,spec=="BG")
dbg3 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
@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}
}