I want to see how many email addresses contain the last name of the email's owner.
Each row in a dataframe contains a last name and an email address. I want to add a third column with a "yes" or a "no" indicating the presence of the last name in the email on that row.
Using a for loop works fine...but I can't help thinking there's probably a better R solution. Any suggestions on how make this more elegant?
vec1 <- c("foo", "smith")
vec2 <- c("sfoo@x.com", "xxx@y.com")
df <- data.frame(vec1,vec2)
for(i in 1:nrow(df)) {
if (grepl(df$vec1[i], df$vec2[i]) == TRUE) {
df$lastNameInEmail[i] <- "Yes"
} else {
df$lastNameInEmail[i] <- "No"
}
}
vec1 vec2 lastNameInEmail
1 foo sfoo@x.com Yes
2 smith xxx@y.com No