从这个L Hernandez开始
从包含以下内容的向量:
[1] "HernandezOlaf " "HernandezLuciano " "HernandezAdrian "
我试过这个:
'subset(ABC, str_detect(ABC, "L Hernandez") == TRUE)'
包括首都L anyplace的Hernandez这个名字是理想的输出.
期望的输出是HernandezLuciano
最佳答案 可能有帮助:
vec1 <- c("L Hernandez", "HernandezOlaf ","HernandezLuciano ", "HernandezAdrian ")
grep("L ?Hernandez|Hernandez ?L",vec1,value=T)
#[1] "L Hernandez" "HernandezLuciano "
更新
variable <- "L Hernandez"
v1 <- gsub(" ", " ?", variable) #replace space with a space and question mark
v2 <- gsub("([[:alpha:]]+) ([[:alpha:]]+)", "\\2 ?\\1", variable) #reverse the order of words in the string and add question mark
您还可以使用strsplit将变量拆分为@rawr注释
grep(paste(v1,v2, sep="|"), vec1,value=T)
#[1] "L Hernandez" "HernandezLuciano "