I am running a for loop with two matrices. One matrix(A) has ~100 strings (such as, name1, name2, …, name100) and only has one column. The other matrix(B) is bigger than A with rows and columns of both values and strings. In some places in B matrix, each name of A matrix is matched. I would like to extract and stack matched entire rows with a particular string of matrix A on output matrix.
So, I am running as below,
output <- NULL
for(K in 1:nrow(A)){
print(K)
for(cc in 1:nrow(B)){
for(dd in 1:ncol(B)){
if(toupper(A[K])==toupper(B[cc,dd])){
output <- rbind(output,B[cc,])
}
}
}
}
But it is too slow. How do you make this for loop more efficient in terms of running time?
Here some idea: