I have two name columns. They look like this
name.a <- c("SPIDER MAN","SUPER MAN","BAT MAN")
name.b <- c("SNOW WHITE","SPIDER MAN","SHELDON COOPER")
y <- cbind(name.a, name.b)
I want to search for each characters from name.a
in name.b
, and return the index. For example, I want to search for "SPIDER MAN" in name.b
, get the result 2. Then do the same for "SUPER MAN" and "BAT MAN".
My failed attempt is
a <- c()
for (i in 1:dim(y)[1]){
for(j in 1:dim(y)[1]){
if (x[,2][j] == x[,1][i]){a <- c(a,i)}
}
}
I have also used grep, but that did not work either.
Please note that I’m trying to search for a list of names. I know how to search for one name using grep, but assuming I have more than 2000 names in name.a, I don’t want to use grep more than 2000 times. When used grep in a for loop, the error I get is “Error in a[i]<-grep(y$name.a[i], y$name.b) :replacement has length zero
So it’s not a duplicate post since the previous doesn’t show how to search for a list of names