2016-07-25 48 views
-2

我有一个叫all.ethnicity的载体。我想grepl(或任何函数)“亚洲”,只有第一个元素成为真,而不是第二个元素“南亚”。注意:我必须使用ethnicity.type对象。grepl使用R中的对象完全匹配字符串

all.ethnicity <- c("Asian", "South Asian", "European") 
ethnicity.type <- "Asian" 
grepl(ethnicity.type,all.ethnicity) 

结果

[1] TRUE FALSE FALSE 
+2

可能更有意义只是做'all.ethnicity == ethnicity.type'尝试 – thelatemail

回答

1

尝试添加开始(^)和结束($)基团到您的图案,例如

all.ethnicity <- c("Asian", "South Asian", "European") 
ethnicity.type <- "^Asian$" 
grepl(ethnicity.type, all.ethnicity) 
[1] TRUE FALSE FALSE 
1

我们可以粘贴^(即字符串的开始)

grepl("^Asian", all.ethnicity) 
相关问题