stringr包中str_detect()
2021-12-20 本文已影响0人
灵活胖子的进步之路
首先看下str_detect()中的参数
str_detect(string,pattern,negate=FALSE)
string:待匹配字符串
pattern:待匹配的模式(需要去查找的字符串)
negate:默认为FALSE,如果为TRUE,则返回没有匹配到的元素返回值是逻辑值
library(tidyverse)
fruit <- c("apple", "banana", "pear", "pinapple")
str_detect(fruit, "apple")#按照关键词查找
str_detect(fruit, "^a")#确定首字母是否为a
str_detect(fruit, "a$")#确定末尾字母是否a
str_detect(fruit, "apple|banana")#对关键词取并集
str_detect(fruit, "apple")#对关键词取并集
#以下取交集
fruit[str_detect(fruit, "apple")]%>%
intersect(.,fruit[str_detect(fruit, "pina")])
#以下取补集
str_detect(fruit, "apple",negate = T)
