Doug Robinson
Doug Robinson

Reputation: 59

Select specific levels of factor in R

I imagine this is a relatively simple issue for this audience, at least it would be for me had I still been working with SAS... I have a character factor ("Trap") with 15 levels and I only want 3 of those levels (Weka, Rat, Stoat) for a data frame I want to summarize/analyze. I don't want to use droplevels and have to write-out every level I DON'T want in the data frame, but I can't seem to get the coding right to select just the levels I want. Please help. Here's what I've most recently tried, to no avail. Any help would be greatly appreciated.

CatchbySpecies <- CatchbySpecies [!CatchbySpecies$Trap == "Weka", "Rat", "Stoat"]

Upvotes: 1

Views: 8548

Answers (2)

linkonabe
linkonabe

Reputation: 846

Should you want to explore data.table. This will reduce your code significantly

library(data.table)
CatchbySpecies <- CatchbySpecies[Trap %in% c("Weka", "Rat", "Stoat")]

You can subset your rows similar to a data.frame

Upvotes: 1

Aaron Montgomery
Aaron Montgomery

Reputation: 1397

From your text, it seems like you want to keep only those three levels -- if that's so, then you want:

CatchbySpecies <- CatchbySpecies [CatchbySpecies$Trap %in% c("Weka", "Rat", "Stoat"), ]

Key differences from your attempt:

  1. No exclamation at the front, unless you want the other ones
  2. Wrap the list of things you do want in the concatenate function, c()
  3. Use %in% instead of ==, since you don't want to check whether the factors are equal to the whole vector of c("Weka", "Rat", "Stoat"), but rather whether the factor is one of those elements contained within
  4. Add a comma at the end. You're imposing a condition on the rows, so use a comma to indicate when that's done, and demonstrate an empty column condition.

Let me know if you have questions!


EDIT: You mentioned not wanting to use droplevels() and I wasn't quite sure why, but Ben Bolker helpfully pointed out that you may want to use it after doing this operation unless you want to retain the discarded factors in this variable for some reason. You can just edit the line as

CatchbySpecies <- 
  droplevels(CatchbySpecies [CatchbySpecies$Trap %in% c("Weka", "Rat", "Stoat"), ])

Upvotes: 3

Related Questions