Q:

Counting values in a matrix conditionally

In R, I am wanting to count the number of different values occurring in a column of a matrix, but only if a certain value occurs in another column.  To clarify, consider this matrix:
MAT <- matrix(nrow=5,ncol=2, c(1,0,1,1,2,1,1,1,2,0))

The matrix looks like this:
 > MAT
     [,1] [,2]
[1,]    1    1
[2,]    0    1
[3,]    1    1
[4,]    1    2
[5,]    2    0

I would like to find the number of '1's occurring in column 2, but only if '0' occurs in column 1 in the same row.  The only function I know which does something similar is table, but I don't think it can check another column; it can only exclude values in the data being checked. ( Please correct me on this if I am wrong.)  I have tried searching on the internet, but I only get hits to unrelated problems. 
Can anyone help me find a function for this problem? 

A:

you can do something like this : 
sum(MAT[,2]==1 & MAT[,1]==0)

A:

You can always subset the matrix with a condition like this:
MAT[ MAT[,1] == 0, ]
table( MAT[ MAT[,1] == 0, ] )

