This is an extension to the question on returning the rows of a matrix that meet a condition in R. Say I have the matrix:
one two three four
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 11 18
[4,] 4 9 11 19
[5,] 5 10 15 20
[6,] 1 6 15 20
[7,] 5 7 12 20
I want to return all rows, where matrix$two == 7
AND matrix$three == 12
as fast as possible. This is the way I know to do it:
out <- mat[mat$two == 7,]
final_out <- out[out$three == 12, ]
There should obviously be a method to get the contents of final_out
in a one-liner, something like: final_out <- which(mat$two == 7 && mat$three == 12)
that is faster and more succinct than the two line of codes above.
What is the fastest R code to return this multiple condition matrix query?