【问题标题】:Remove .x from a column从列中删除 .x
【发布时间】:2016-06-01 11:32:36
【问题描述】:

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

Human_ortholog  Representative_transcript
FAM126A         ENST00000409923.1
CYP3A5          ENST00000339843.2
LCMT1           ENST00000399069.3
SPATA31A6       ENST00000332857.6

如何使用 gsub 删除 .n,其中 n 是 . 之后的数字?

【问题讨论】:

    标签: r regex bioinformatics gsub


    【解决方案1】:

    方法如下:

    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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 2023-02-16
      • 1970-01-01
      • 2021-02-16
      相关资源
      最近更新 更多