Aparanji Aparanji - 2 months ago 6
R Question

How to find the row number of a matrix when its two columns match the given values in r

I have a 4X5 matrix example:

a = 1 2 3 4 5
2 3 3 4 1
5 6 77 8 9
10 9 3 4 17


Here I want to know the row number where
a[,3] == 3
and
a[,4] == 4
.
I am suppose to get the answer as 1,2 and 4
How do I get it in r

Answer

use this

a = matrix(c(1,2,3,4,5,2,3,3,4,1,5,6,77,8,9,10,9,3,4,17), byrow = T, ncol = 5)
which(a[, 3] == 3 & a[ ,4] == 4)

[1] 1 2 4

Comments