Delete a period and a number at the end of a character string
You just need a simple regular expression:
z_new = gsub("\\.[0-9]*$", "", z)
A few comments:
- The first argument in gsub is the pattern we are looking for. The second argument is what to replace it with (in this case, nothing).
- The
$
character looks for the pattern at the end of the string [0-9]*
looks for 1 or more digits. Alternatively, you could use\\d*
or[[:digit:]]*
.\\.
matches the full stop. We need to escape the full stop with two slashes.
Try this
gsub("\\.[[:digit:]]*$", "", z)