user18723720
user18723720

Reputation: 21

Removing rows in R dataframe based on condition

I have an issue with some data analysis I am doing. I have a dataset covering cricket matches, and therefore have a dataset with the variable "Overs". I wish to elimate the games which have been played with less than 35 overs by both teams. An example of mty data :

Match_ID   Country    Overs
1          England    50
1          Australia  50
2          Pakistan   50
2          India      32
3          England    30
3          Pakistan   30 

In this scenario, I would only like to remove Match ID 3, as both teams have played less than 35 overs, but not Match ID 2 as only one of the teams have played less than 35 overs. This is how I would like my data to look: Any tips?

Match_ID   Country    Overs
1          England    50
1          Australia  50
2          Pakistan   50
2          India      32

Upvotes: 0

Views: 1153

Answers (1)

IceCreamToucan
IceCreamToucan

Reputation: 28675

df <- structure(list(Match_ID = c(1L, 1L, 2L, 2L, 3L, 3L), Country = c("England", 
"Australia", "Pakistan", "India", "England", "Pakistan"), Overs = c(50L, 
50L, 50L, 32L, 30L, 30L)), row.names = c(NA, -6L), class = "data.frame")

library(dplyr, warn = FALSE)

df %>% 
  group_by(Match_ID) %>% 
  filter(!all(Overs < 35)) %>% 
  ungroup()
#> # A tibble: 4 × 3
#>   Match_ID Country   Overs
#>      <int> <chr>     <int>
#> 1        1 England      50
#> 2        1 Australia    50
#> 3        2 Pakistan     50
#> 4        2 India        32

Created on 2022-09-12 with reprex v2.0.2

Upvotes: 1

Related Questions