Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing rows in R based on values in a single column

Tags:

r

I have a n x 3 matrix in R and want to remove all rows where the last column is less than x. What is the best way to do this?

like image 835
David Avatar asked Apr 20 '12 19:04

David


People also ask

How do I drop a row based on a column value in R?

To remove rows with an in R we can use the na. omit() and <code>drop_na()</code> (tidyr) functions. For example, na.

How do I remove rows from two conditions in R?

To remove rows of data from a dataframe based on multiple conditional statements. We use square brackets [ ] with the dataframe and put multiple conditional statements along with AND or OR operator inside it. This slices the dataframe and removes all the rows that do not satisfy the given conditions.

How do you omit rows with NA in R?

To remove all rows having NA, we can use na. omit function. For Example, if we have a data frame called df that contains some NA values then we can remove all rows that contains at least one NA by using the command na. omit(df).


3 Answers

You could also use the subset() function.

a <- matrix(1:9, nrow=3)  
threshhold <- 8  
subset(a, a[ , 3] < threshhold)  
like image 98
johannes Avatar answered Sep 19 '22 15:09

johannes


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
like image 29
Ben Avatar answered Sep 19 '22 15:09

Ben


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).

like image 44
Jeff Allen Avatar answered Sep 18 '22 15:09

Jeff Allen