Ktal
Ktal

Reputation: 25

Take out part of the string from part of vector in R

I have this question tidying up the data. I have a vector called "opponent" like this:

vs. wakeforest
Utah
vs. [9] Los Angelas
Seattle
vs. [11] UNC
at [19] North Carolina

I only want the opponent name, that is:

wakeforest
Utah
Los Angelas
Seattle
UNC
North Carolina

How should I clean it up? Thanks a lot!

Upvotes: 1

Views: 35

Answers (1)

akrun
akrun

Reputation: 886938

We can match characters until the . or ] followed by one or more space (\\s+) and replace it with ''.

sub('^.*(\\.|\\])\\s+', '', opponent)
#[1] "wakeforest"     "Utah"           "Los Angelas"    "Seattle"       
#[5] "UNC"            "North Carolina"

data

opponent <- c('vs. wakeforest', 'Utah',
'vs. [9] Los Angelas', 'Seattle',
'vs. [11] UNC','at [19] North Carolina')

Upvotes: 3

Related Questions