Sahil Talwar
Sahil Talwar

Reputation: 13

excluding a set of observations in subsetting

Suppose I have a data on characteristics of organisms. I want to subset the data on the basis of species. I have under species 50 types of species and I want to subset by excluding one species for example amphibians. What command can help me do that ? One way to do it is by writing explicitly the names of all the species explicitly in the subset command as follows

sub <- subset(data, species %in% c("species1,species2,...,species50))  #all species excluding amplhibians.

But this will be a very hectic process. How can I use the subset command to get my result ? Thanks in advance.

Upvotes: 0

Views: 859

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

Simple: just negate the test:

sub <- subset(data, ! species %in% 'amphibians')

Or, since you only want to exclude one species:

sub <- subset(data, species != 'amphibians')

Note that prefix-! has a different operator precedence in R than in other languages: in almost all other programming languages, the first piece of code would require parentheses around the species %in% 'amphibians' test. In R, this isn’t necessary: ! a %in% b is equivalent to ! (a %in% b).

Upvotes: 2

Related Questions