user1987607
user1987607

Reputation: 2157

R sub extract everything before last occurence of a character

I have the following vector c:

ABC-XXX
DEF-4-YYY

I want to extract everything before the last occurence of '-', meaning that I would keep this

ABC
DEF-4

I've tried the following:

sub([-].*, '', "DEF-4-YYY")

But this replaces everything after the first '-', while it should look for the last '-'. Output of the above command is:

"DEF"

What is wrong here?

Upvotes: 13

Views: 4764

Answers (2)

akrun
akrun

Reputation: 886998

We can do with sub by matching a - followed by zero or more characters that are not a - till the end ($) of the string and replace it with blank ('')

sub('-[^-]*$', '', v1)
#[1] "ABC"   "DEF-4"

data

v1 <- c('ABC-XXX', 'DEF-4-YYY')

Upvotes: 19

Antonios
Antonios

Reputation: 1939

if you prefer you can use stringr package

library(stringr)
matches=str_locate_all(c,"-")
chars=sapply(matches,function(x) x[nrow(x),])[1,]
str_sub(c,1,chars-1)

Upvotes: 4

Related Questions