Velvetaura
Velvetaura

Reputation: 47

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.

Upvotes: 2

Views: 460

Answers (2)

akrun
akrun

Reputation: 886938

In base R, use subset

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

Upvotes: 2

TTS
TTS

Reputation: 1928

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)

Upvotes: 2

Related Questions