从列中删除 .x
Remove .x from a column
我有一个 table,前两列如下所示:
Human_ortholog Representative_transcript
FAM126A ENST00000409923.1
CYP3A5 ENST00000339843.2
LCMT1 ENST00000399069.3
SPATA31A6 ENST00000332857.6
如何使用 gsub 删除 .n
,其中 n 是 .
之后的数字?
方法如下:
df$col = sub('\.\d$', '', df$col)
这会删除字符串末尾的一个数字,前面有一个点。如果数字可以包含多个数字,请使用适当的量词:
df$col = sub('\.\d+$', '', df$col)
此答案使用 sub
,因为您只想对每个字符串执行一次替换。 gsub
在对每个字符串执行多次替换时(仅)有意义,如本例所示:
sub('[aeiou]', '', 'This is a test')
# [1] "Ths is a test"
gsub('[aeiou]', '', 'This is a test')
# [1] "Ths s tst"
我有一个 table,前两列如下所示:
Human_ortholog Representative_transcript
FAM126A ENST00000409923.1
CYP3A5 ENST00000339843.2
LCMT1 ENST00000399069.3
SPATA31A6 ENST00000332857.6
如何使用 gsub 删除 .n
,其中 n 是 .
之后的数字?
方法如下:
df$col = sub('\.\d$', '', df$col)
这会删除字符串末尾的一个数字,前面有一个点。如果数字可以包含多个数字,请使用适当的量词:
df$col = sub('\.\d+$', '', df$col)
此答案使用 sub
,因为您只想对每个字符串执行一次替换。 gsub
在对每个字符串执行多次替换时(仅)有意义,如本例所示:
sub('[aeiou]', '', 'This is a test')
# [1] "Ths is a test"
gsub('[aeiou]', '', 'This is a test')
# [1] "Ths s tst"