Reputation: 31
The values in my column is a combination of letters and digits, which looks like this: abc1237pqr, 413ogty, ptw569q, qrt
.
How do I remove all the characters after the appearance of the last digit so that it becomes: abc1237, 413, ptw569, qrt
? That is, the letters in the beginning are retained, but not the ones that appear after the digits.
Upvotes: 2
Views: 1683
Reputation: 886978
We can use sub
to capture a digit ((\\d)
) followed by one or more characters that are not a digit ([^0-9]+
) until the end of the string ($
) and replace with the backreference (\\1
) of the captured group
sub("(\\d)[^0-9]+$", "\\1", df1$v1)
#[1] "abc1237" "413" "ptw569" "qrt"
df1 <- data.frame(v1 =c('abc1237pqr', '413ogty', 'ptw569q', 'qrt'), stringsAsFactors = FALSE)
Upvotes: 3