Search code examples
rconditional-statements

How to remove rows from dataframe based upon criteria in R?


I have a dataframe with thousands of observations, and a few variables. I want to create a subset of this dataframe whereby I remove rows of no interest to me, based upon multiple conditions working in union.

For example if I wanted to perform the following:

Remove entries where event_type = 'A' or 'T' and value < 0,

how would one format this in R? (Where event_type and value are variables) Many thanks.


Solution

  • Here's a dplyr response. Simply filter the data for the conditions indicated and the result should be what you're looking for.

    library(dplyr)
    
    df_filtered <- df %>%
       filter(event_type %in% c('A', 'T'),
              value < 0)