J.Homer
J.Homer

Reputation: 31

R: Find where one variables contains the value of another

Working in R. I want to look at one variable to see if it contains the contents of another. This needs to be done element by element.

df1 <- data.frame(
         FirstName = c("Jon", "Rob", "Phil", "Andy"),
         BusName = c("Jon's Auto", "123 Taxis", "Plumber Phil", "Handy 
Plumbers")

I have tried lots of variations of grep/grepl/match/%in%/sub but this seems to only match a static string against the variable. eg

grep("Jon",df1$BusName) would give me TRUE,FALSE,FALSE,FALSE but 
grep(df1$FirstName, df1$BusNAme) gives an error

I want to scan down and compare them element by element so the output would be TRUE, FALSE, TRUE, FALSE

Upvotes: 3

Views: 2115

Answers (2)

Anders Ellern Bilgrau
Anders Ellern Bilgrau

Reputation: 10223

In base R, you can use mapply and grepl to "vectorize" over the arguments:

df1 <- data.frame(
  FirstName = c("Jon", "Rob", "Phil", "Andy"),
  BusName = c("Jon's Auto", "123 Taxis", "Plumber Phil", "Handy Plumbers"),
  stringsAsFactors = FALSE
)

df1$match <- mapply(grepl, df1$FirstName, df1$BusName)
print(df1$match)
#  Jon   Rob  Phil  Andy 
# TRUE FALSE  TRUE FALSE 

Upvotes: 2

akrun
akrun

Reputation: 887118

We can convert the pattern column to character. Note that grep is not vectorized for pattern. Using str_detect

library(stringr)
str_detect(df1$BusName, as.character(df1$FirstName))
#[1]  TRUE FALSE  TRUE FALSE

Upvotes: 4

Related Questions