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

Time:06-28

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.

CodePudding user response:

In base R, use subset

subset(df1, event_type %in% c("A", "T") & value < 0)

CodePudding user response:

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)
  • Related