Reputation: 59
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
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
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:
c()
%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 withinLet 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