保留具有特定结尾的行

时间:2018-10-26 09:11:02

标签: r

我在data.frame中具有这样的行名,并且只想保留带有p__后跟字母[A-z]而没有|符号的行名。 试过这个,但是没用:

grep("p__[[:alpha:]]$",rownames(df),  perl=T)

输入:

p__xxxx|g_xxxx|s_xxxx
p__xxxx|g_xxxx
p__xxxx

输出:

p__xxxx

2 个答案:

答案 0 :(得分:0)

您需要使用+指定下划线后可以有多个字母。

x <- c('p__xxxx|g_xxxx|s_xxxx', 'p__xxxx|g_xxxx', 'p__xxxx')
grep("p__[[:alpha:]]+$",x)
[1] 3

答案 1 :(得分:0)

假设您有一个包含3个字符串的向量:

str0<-"p__xxxx|g_xxxx|s_xxxx"
str1<-"p__xxxx|g_xxxx"
str2<-"p__xxxx"

mystr<-c(str0,str1,str2)

然后您可以使用以下选项进行选择:

mystr[!grepl('\\|', mystr)&grepl("p__",mystr)]
[1] "p__xxxx"