Search code examples
rmatrixrowmembership

Checking row membership of a matrix


I have a matrix in which each row vector has a name. I would like to check row membership in my matrix, that is I would like to turn the following into R code:

if(mat contains "rowname")
{  do appropriate task ....}
else if(mat contains "otherrowname")
{  do appropriate task ....}
else
{  do different task....}
  1. How can I test for row membership in a matrix?

All help is appreciated!


Solution

  • It is fairly common to see code that looks like:

     if( sum( rowNameToFind %in% rownames(mat)) ) { TRUE }else{ FALSE }
    

    This deals simultaneously with the rownames-missing-entirely possibility at the same time as target-not-in-rownames.