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?
Just use [
subsetting with logical comparison...
# Reproducible data
set.seed(1)
m <- matrix( sample(12,28,repl=T) , 7 , 4 )
[,1] [,2] [,3] [,4]
[1,] 4 8 10 3
[2,] 5 8 6 8
[3,] 7 1 9 2
[4,] 11 3 12 4
[5,] 3 3 5 5
[6,] 11 9 10 1
[7,] 12 5 12 5
# Subset according to condition
m[ m[,2] == 3 & m[,3] == 12 , ]
[1] 11 3 12 4