How do I delete a number (one or two digits in length) and the dot directly preceding it each time it occurs at the end of a string within a specific variable in
z<-c("awe", "p.56.red.45", "ted.5", "you.88.tom")
You just need a simple regular expression:
z_new = gsub("\\.[0-9]*$", "", z)
A few comments:
$character looks for the pattern at the end of the string
[0-9]*looks for 1 or more digits. Alternatively, you could use
\\.matches the full stop. We need to escape the full stop with two slashes.