从字符串末尾删除句点 [英] Removing period from the end of string
本文介绍了从字符串末尾删除句点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些值,其中有些值最后有一个句点 (.
),这里是 95.2.和 87.5.:
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)
推荐答案
我们可以使用 sub
来匹配点 (.
- 是表示任何字符的元字符 - 所以我们要么转义 (\\.
) 或将其放在方括号中以评估为点字符)在字符串的末尾 ($
) 并将其替换为空格,然后将其转换为 numeric
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))
这篇关于从字符串末尾删除句点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文