swcarpentry / r-novice-gapminder

R for Reproducible Scientific Analysis
http://swcarpentry.github.io/r-novice-gapminder/
Other
164 stars 537 forks source link

Subsetting data episode: dropping levels of a factor #741

Open gtitcomb opened 3 years ago

gtitcomb commented 3 years ago

In this episode, participants are shown various examples of subsetting a factor in the "Factor subsetting" section. One of the last comments is "Skipping elements will not remove the level even if no more of that category exists in the factor:" f[-3] I felt that this comment begs the question: How do we drop the levels of a factor? Perhaps a line of code stating that levels can be dropped using: droplevels(f[-3]) would help clear this question up, hopefully without introducing too much additional content.

I'm not sure how widely-used droplevels is now that dplyr is the preferred way to manipulate/group data, but perhaps it's useful if a user is importing some files with empty levels, or even if someone tries to write a loop by iterating over levels of a factor.

Alternatively, the comment about skipping elements of the level could be removed if it is not revisited later in the lesson or expanded upon here.

jcoliver commented 2 years ago

Thanks, @gtitcomb . A brief point about the functionality of droplevels() might be most appropriate as a Tip immediately following the f[-3] output.