根据前缀和后缀对字符串进行子集化 [英] Subsetting a string based on pre- and suffix
本文介绍了根据前缀和后缀对字符串进行子集化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一列具有以下类型的名称:
I have a column with these type of names:
sp_O00168_PLM_HUMAM
sp_Q8N1D5_CA158_HUMAN
sp_Q15818_NPTX1_HUMAN
tr_Q6FGH5_Q6FGH5_HUMAN
sp_Q9UJ99_CAD22_HUMAN
我想删除之前的所有内容,包括第二个 _ 和之后的所有内容,包括第三个 _.
I want to remove everything before, and including, the second _ and everything after, and including, the third _.
我不会根据字符数删除哪个,因为这不是一个固定的数字.
I do not which to remove based on number of characters, since this is not a fixed number.
输出应该是:
PLM
CA158
NPTX1
Q6FGH5
CAD22
我玩过这些,但不太正确..
图书馆(纵梁)
str_sub(x,-6,-1)
I have played around with these, but don't quite get it right..
library(stringer)
str_sub(x,-6,-1)
推荐答案
> gsub(".*_.*_(.*)_.*", "\\1", "sp_O00168_PLM_HUMAM")
[1] "PLM"
这篇关于根据前缀和后缀对字符串进行子集化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文