Select rows of a matrix that meet a condition

前端 未结 6 613
半阙折子戏
半阙折子戏 2020-11-29 15:26

In R with a 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         


        
6条回答
  •  醉梦人生
    2020-11-29 15:48

    This is easier to do if you convert your matrix to a data frame using as.data.frame(). In that case the previous answers (using subset or m$three) will work, otherwise they will not.

    To perform the operation on a matrix, you can define a column by name:

    m[m[, "three"] == 11,]
    

    Or by number:

    m[m[,3] == 11,]
    

    Note that if only one row matches, the result is an integer vector, not a matrix.

提交回复
热议问题