Removing rows in R based on values in a single column Removing rows in R based on values in a single column r r

Removing rows in R based on values in a single column


You could also use the subset() function.

a <- matrix(1:9, nrow=3)  threshhold <- 8  subset(a, a[ , 3] < threshhold)  


Same approach as @JeffAllen but in a little more detail and generalisable to a matrix of any size.

    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


m <- matrix(rnorm(9), ncol=3)m <- m[m[,3]>0,]

Creates a matrix, then redefines that matrix only to include those rows in which the third column is greater than 0 (m[,3] > 0).