简体   繁体   English

如何删除R中字符串中的一些字符

[英]how to remove some characters in string in R

I analyse some gene id. 我分析了一些基因id。 these Ids similar as follow: 这些ID类似如下:

"ENSG00000189001.9" "ENSG00000179152.17" "ENSG00000131374.13" “ENSG00000189001.9”“ENSG00000179152.17”“ENSG00000131374.13”

1- I would like to remove '.' 1-我想删除'。' and every character after that such as follow: 以及之后的每个角色如下:

"ENSG00000189001" "ENSG00000179152" "ENSG00000131374" “ENSG00000189001”“ENSG00000179152”“ENSG00000131374”

2-After step1, I would like to remove "" from my string such as below: 2-step1之后,我想从我的字符串中删除“”,如下所示:

ENSG00000189001 ENSG00000179152 ENSG00000131374 ENSG00000189001 ENSG00000179152 ENSG00000131374

You can do this using gsub 你可以使用gsub来做到这一点

GID = c("ENSG00000189001.9", "ENSG00000179152.17", "ENSG00000131374.13")
GID2 = gsub("\\..*", "", GID)
cat(GID2, "\n")
ENSG00000189001 ENSG00000179152 ENSG00000131374 

Note that if you just type GID2, you will still see the quotes. 请注意,如果您只输入GID2,您仍会看到引号。 That is just how R indicates that these are strings. 这就是R表明这些是字符串的方式。 Using cat shows only the string contents 使用cat仅显示字符串内容

A little detail about the regular expression: 关于正则表达式的一些细节:
\\\\. matches the first period found in each string. 匹配每个字符串中找到的第一个句点。
.* matches everything after that. .*匹配之后的所有内容。
gsub will replace the matched part (period and everything after) with "", ie the empty string. gsub将用“”替换匹配的部分(句点和后面的所有内容),即空字符串。

Here are a few fun, out of the ordinary ways to get the desired result. 这里有一些有趣的,与众不同的方法来获得理想的结果。

scan(text=GID, what="", comment.char=".")
# Read 3 items
# [1] "ENSG00000189001" "ENSG00000179152" "ENSG00000131374"
dirname(chartr(".", "/", GID))
# [1] "ENSG00000189001" "ENSG00000179152" "ENSG00000131374"
read.table(text=GID, sep=".", stringsAsFactors=FALSE)$V1
# [1] "ENSG00000189001" "ENSG00000179152" "ENSG00000131374"
stringi::stri_split_fixed(GID, ".", simplify=TRUE)[,1]
# [1] "ENSG00000189001" "ENSG00000179152" "ENSG00000131374"

Data: 数据:

GID <- c("ENSG00000189001.9", "ENSG00000179152.17", "ENSG00000131374.13")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM