Luke
Luke

Reputation: 4929

Exclude rows by identifying a sequence using subset()

I am trying to exclude a series of rows from a dataset by using the subset() command by identifying a sequence of numbers in the "Rec" column that I want to remove. My attempts to use : and > within subset have failed, for example:

dataset<-subset(dataset,Rec !1812:1843) #here I'd like to exclude all rows with values of 1812:1843 for Rec in the dataset

or

dataset<-subset(dataset,Rec !>1812) #here I'd like to exclude all rows with Rec>1812

Can someone show me how to use the <> and : operators in this way? Can it be done with subset()?

Upvotes: 1

Views: 1756

Answers (1)

tkmckenzie
tkmckenzie

Reputation: 1363

For inclusion/exclusion based on membership in a list in general, you can use the %in% operator:

dataset <- subset(dataset, !(Rec %in% 1812:1843))

Upvotes: 4

Related Questions