根据单个列中的值删除R中的行

Dav*_*vid 16 r

我在R中有3个矩阵,想要删除最后一列小于x的所有行.做这个的最好方式是什么?

joh*_*nes 13

您也可以使用该subset()功能.

a <- matrix(1:9, nrow=3)  
threshhold <- 8  
subset(a, a[ , 3] < threshhold)  
Run Code Online (Sandbox Code Playgroud)


Ben*_*Ben 5

与@JeffAllen相同的方法,但更详细一点,并且可以适用于任何大小的矩阵.

    data <- rbind(c(1,2,3), c(1, 7, 4), c(4,6,7), c(3, 3, 3), c(4, 8, 6))
    data
         [,1] [,2] [,3]
    [1,]    1    2    3
    [2,]    1    7    4
    [3,]    4    6    7
    [4,]    3    3    3
    [5,]    4    8    6
    #
    # set value of x
    x <- 3
    # 
    # return matrix that contains only those rows where value in 
    # the final column is greater than x. 
    # This will scale up to a matrix of any size 
    data[data[,ncol(data)]>x,]
         [,1] [,2] [,3]
    [1,]    1    7    4
    [2,]    4    6    7
    [3,]    4    8    6
Run Code Online (Sandbox Code Playgroud)