Garrus
Garrus

Reputation: 43

Remove strings from a vector in R that match a rule

I have a vector of strings, for example a list of states:

USstates<-rownames(USArrests)

I would like to remove some States from the vector using a rule. For instance, States that contains a double n in the name: Pennsylvanya, Tennessee, Connecticut, Minnesota

How do I go about it?

Upvotes: 0

Views: 42

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521053

You may try:

USstates_nn <- USstates[!grepl("nn", USstates)]

Upvotes: 2

akrun
akrun

Reputation: 887048

We may use grep with invert as TRUE

USstates_nn <- USstates[grep("nn", USstates, invert = TRUE)]

Upvotes: 1

Related Questions