简体   繁体   中英

recoding a string vector using multiple words

I have the following two vectors:

vector1 <- c("Canada", "EEUU", "EE UU", "Uruguay", "Madrid", "Peru", "Chile")   
vector2 <- c("EEUU", "EE UU", "Madrid")

I want to find if the vector1 matches at least one word from vector2. The outcome should be:

# FALSE TRUE TRUE FALSE TRUE FALSE FALSE

I know how to do it one by one,

vector3 <- "EEUU"
str_detect(vector1, vector3)
# FALSE TRUE FALSE FALSE FALSE FALSE FALSE

But there must be a way to do it all at the same time. I know it's a simple question, but I haven't find an answer that addresses it directly.

Many thanks,

Try this

vector1 %in% vector2

also read ?match and if you're looking to set operations, read ?union and ?intersect

您正在寻找match功能(或其快捷方式%in% ): http : //stat.ethz.ch/R-manual/R-patched/library/base/html/match.html

vector1 %in% vector2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM