I have a table with first two columns that looks like this:
Human_ortholog Representative_transcript
FAM126A ENST00000409923.1
CYP3A5 ENST00000339843.2
LCMT1 ENST00000399069.3
SPATA31A6 ENST00000332857.6
How can I remove the .n where n is the number after ., using gsub?
Here’s how:
df$col = sub('\\.\\d$', '', df$col)
This removes a single digit, preceded by a dot, at the end of the string. If the numbers could consist of multiple digits, use an appropriate quantifier:
df$col = sub('\\.\\d+$', '', df$col)
This answer is using sub since you only want to perform a single replacement per string. gsub makes sense (only) when performing multiple replacements per string, as in this example:
sub('[aeiou]', '', 'This is a test')
# [1] "Ths is a test"
gsub('[aeiou]', '', 'This is a test')
# [1] "Ths s tst"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With