alwaysaskingquestions
alwaysaskingquestions

Reputation: 1657

Check which words show up at least once within words from another vector

Let's say we have a list of words:

words = c("happy","like","chill")

Now I have another string variable:

s = "happyMeal"

I wanted to check which word in words has the matching part in s. So s could be "happyTime", "happyFace", "happyHour", but as long as there's "happy" in there, I want my result to return the index of word "happy" in the string vector words.

This question is similar but not identical to from the question asked in the post: Find a string in another string in R.

Upvotes: 1

Views: 88

Answers (2)

josliber
josliber

Reputation: 44310

You can loop through each of the words that you're searching for with sapply, using grepl to determine if that word appears in s:

sapply(words, grepl, s)
# happy  like chill 
#  TRUE FALSE FALSE 

If s is a single word then sapply with grepl returns a logical vector that you can use to determine the words that matched:

words[sapply(words, grepl, s)]
# [1] "happy"

When s contains multiple words, then sapply with grepl returns a logical matrix, and you can use the column sums to determine which words showed up at least once:

s <- c("happyTime", "chilling", "happyFace")
words[colSums(sapply(words, grepl, s)) > 0]
# [1] "happy" "chill"

Upvotes: 2

akrun
akrun

Reputation: 887068

Here is an option using stri_detect from stringi

library(stringi)
words[stri_detect_regex(s, words)]
#[1] "happy"

Upvotes: 1

Related Questions