applicant
applicant

Reputation: 105

Extracting data from vector according to a rule in R

I am trying to extract the names of the countries which don't start with either "C" or "B" from this vector.

vec <- c("Colombia", "Chile", "Brazil", "Ecuador", "Peru", "Mexico")

This works for C or B only:

vec[substring(vec,1,1) != 'C']

I am trying to combine both cases but this doesn't work

vec[(substring(vec,1,1) != 'C') | (substring(vec,1,1) != 'P')]

How can I combine both conditions in just one statement?

Upvotes: 0

Views: 46

Answers (2)

Jul
Jul

Reputation: 1139

You can use grep for this:

vec[grep("^[^CP]",vec)]

Upvotes: 1

Kevin Arseneau
Kevin Arseneau

Reputation: 6264

Isn't this what you are looking for?

vec[!substring(vec, 1, 1) %in% c("B", "C")]

Upvotes: 2

Related Questions