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.
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
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))})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With