Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing period from the end of string

Tags:

regex

r

I have values where some have a period (.) in the end, here 95.2. and 87.5.:

c("25.3", "95.4", "95.6", "95.2.", "87.5.")

How can I remove the trailing periods? Desired result:

c(25.3, 95.4, 95.6, 95.2, 87.5)

like image 400
Toney Honar Avatar asked Dec 17 '25 10:12

Toney Honar


1 Answers

We can use sub to match the dot (. - is a metacharacter meaning any character - so we either escape (\\.) or place it in a square bracket to evaluate as the dot character) at the end ($) of the string and replace it with blanks, then convert it to numeric

as.numeric(sub("[.]$", "", dat$Col))
like image 51
akrun Avatar answered Dec 20 '25 09:12

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!