从列中删除.x

Kis*_*sun 3 regex r bioinformatics gsub

我有一个前两列的表,如下所示:

Human_ortholog  Representative_transcript
FAM126A         ENST00000409923.1
CYP3A5          ENST00000339843.2
LCMT1           ENST00000399069.3
SPATA31A6       ENST00000332857.6
Run Code Online (Sandbox Code Playgroud)

如何使用gsub 删除.nwhere后面的数字.

Kon*_*lph 5

这是如何做:

df$col = sub('\\.\\d$', '', df$col)
Run Code Online (Sandbox Code Playgroud)

这将删除字符串末尾的单个数字,前面有一个点.如果数字可能包含多个数字,请使用适当的量词:

df$col = sub('\\.\\d+$', '', df$col)
Run Code Online (Sandbox Code Playgroud)

这个答案正在使用,sub因为您只想为每个字符串执行一次替换.gsub在每个字符串执行多次替换时有意义(仅限),如下例所示:

sub('[aeiou]', '', 'This is a test')
# [1] "Ths is a test"
gsub('[aeiou]', '', 'This is a test')
# [1] "Ths s  tst"
Run Code Online (Sandbox Code Playgroud)