I have the following line
string <- c('a - b - c - d',
'z - c - b',
'y',
'u - z')
I would like to multiply it so that everything after the second appearance of “-” is thrown away.
The result is the following:
> string
[1] "a - b" "z - c" "y" "u - z"
I used substr(x = string, 1, regexpr(string, pattern = '[^ - ]*$') - 4), but excludes the last occurrence of '-', which I do not want.
source
share