Assume I have a matrix:
> mat1
A B
A 1 3
B 2 4
And a data frame:
> df
First Second
1 A A
2 A B
3 B A
4 B B
How do I do a lookup for the value of First and Second in the matrix?
My desired output in this case is:
> df
First Second result
1 A A 1
2 A B 3
3 B A 2
4 B B 4
I figured out that I can do df <- cbind(df, result = diag(mat1[df$First, df$Second])) but is there a way to do it without the diag()? I figured that if I have a long dataframe, then mat1[df$First, df$Second] will make the code run slower if it generates a larger matrix.