Pregunta

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,

¿Fue útil?

Solución

You're looking for the match function (or its shortcut %in%): http://stat.ethz.ch/R-manual/R-patched/library/base/html/match.html

vector1 %in% vector2

Otros consejos

Try this

vector1 %in% vector2

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top