Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove .x from a column

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?

like image 442
Kisun Avatar asked Apr 24 '26 02:04

Kisun


1 Answers

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"
like image 133
Konrad Rudolph Avatar answered Apr 25 '26 17:04

Konrad Rudolph



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!