Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split Character String Using Only Last Delimiter in r

Tags:

r

I have a character variable that I would like to split into 2 variables based on a "-" delimiter, however, I would only like to split based on the last delimiter as there might be multiple "-" in the string. Example:

Input          Output1  Output2
foo - bar      foo      bar
hey-now-man    hey-now  man
say-now-girl   say-now  girl
fine-now       fine     now

I've tried using strsplit to no avail.

like image 483
ericbrownaustin Avatar asked Sep 12 '25 13:09

ericbrownaustin


2 Answers

You can also use a negative lookahead:

df <- tibble(input = c("foo - bar", "hey-now-man", "say-now-girl", "fine-now"))

df %>% 
    separate(input, into = c("output1", "output2"), sep = "\\-(?!.*-)", remove = FALSE)

Refs:

[1] https://frightanic.com/software-development/regex-match-last-occurrence/

[2] https://www.regular-expressions.info/lookaround.html

like image 65
Joao Paulo Nogueira Avatar answered Sep 15 '25 03:09

Joao Paulo Nogueira


You can try using gregexpr :

a=c("foo - bar","hey-now-man","say-now-girl","fine-now")
lastdelim = tail(gregexpr("-",a)[[1]],n=1)
output1 = sapply(a,function(x) {substr(x,1,lastdelim-1)})
output2 = sapply(a,function(x) {substr(x,lastdelim+1,nchar(x))})
like image 33
Bea Avatar answered Sep 15 '25 04:09

Bea