从R中提取以某种模式开始的元素

时间:2016-03-22 16:27:32

标签: r grep data-manipulation grepl

我想选择以x2开头的Loc元素。我不知道我在这里做错了什么。

x2 <- c("(Intercept)", "Conc", "LocKhanewal", "LocLAB-PK", "LocLayyah", 
        "LocMultan", "LocToba Tek Singh", "LocVehari", "Conc:LocKhanewal", 
        "Conc:LocLAB-PK", "Conc:LocLayyah", "Conc:LocMultan", "Conc:LocToba Tek Singh", 
        "Conc:LocVehari")
x2

[1] "(Intercept)"            "Conc"                   "LocKhanewal"            "LocLAB-PK"             
[5] "LocLayyah"              "LocMultan"              "LocToba Tek Singh"      "LocVehari"             
[9] "Conc:LocKhanewal"       "Conc:LocLAB-PK"         "Conc:LocLayyah"         "Conc:LocMultan"        
[13] "Conc:LocToba Tek Singh" "Conc:LocVehari"   

grepl("^Loc.$", x2)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

但是,相同的代码在这里工作正常。

x1 <- c("(Intercept)", "Conc", "TypeB", "TypeC", "Conc:TypeB", "Conc:TypeC")

x1
[1] "(Intercept)" "Conc"        "TypeB"       "TypeC"       "Conc:TypeB"  "Conc:TypeC"

grepl("^Type.$", x1)
[1] FALSE FALSE  TRUE  TRUE FALSE FALSE

1 个答案:

答案 0 :(得分:2)

我们需要使用.*来匹配所有字符,直到字符串的结尾($)。在第二种情况下(幸运或不幸的是),我们在&#34; Type&#34;之后只有一个字符。因此,grep为这些提供了TRUE输出。对于&#34; Loc&#34;,它是不同的,它有多个字符

grepl("^Loc.*$", x2)
#[1] FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE