删除“。”后的部分string
这是一个简单的问题,但我不明白我做错了什么。 我工作在R与login号如variablesa
>a <- c("NM_020506.1","NM_020519.1","NM_001030297.2","NM_010281.2","NM_011419.3", "NM_053155.2")
要从biomart软件包中获取信息,我需要删除.1等。 我通常这样做这个代码:
> b <- sub("..*","",a) >[1] "" "" "" "" "" ""
但是正如你所看到的,这不是这个variables的正确方法。 谁能帮我这个?
你只需要逃避一段时间:
a <- c("NM_020506.1","NM_020519.1","NM_001030297.2","NM_010281.2","NM_011419.3", "NM_053155.2") gsub("\\..*","",a) [1] "NM_020506" "NM_020519" "NM_001030297" "NM_010281" "NM_011419" "NM_053155"
你可以这样做:
sub("*\\.[0-9]", "", a)
要么
library(stringr) str_sub(a, start=1, end=-3)
我们可以假装他们是文件名并删除扩展名 :
tools::file_path_sans_ext(a) # [1] "NM_020506" "NM_020519" "NM_001030297" "NM_010281" "NM_011419" "NM_053155"