0

I am trying to filter out groups based on the condition that the group does not contain deviant – see the following dataset:

df <- structure(list(
  group_id = c(1, 1, 1, 2, 2, 2, 3, 3, 3), 
  sound = c("standard", "standard", "deviant", "standard", " standard ", " standard ", "deviant", " standard", " standard")),
  .Names = c("group_id", "soudn"),
  row.names = c(NA, -9L),
  class = "data.frame")

So I want to keep all of groups 1 and 3 as deviant is present in both (and this is the condition we are looking for), but remove group 2 since deviant isn’t present.

Maël
  • 45,206
  • 3
  • 29
  • 67
S2068
  • 33
  • 4
  • 1
    Does this answer your question? [Select groups which have at least one of a certain value](https://stackoverflow.com/questions/40825037/select-groups-which-have-at-least-one-of-a-certain-value) – Maël Dec 23 '22 at 15:17

1 Answers1

0
library(dplyr)
df %>% 
  group_by(group_id) %>% 
  filter(any(soudn == "deviant"))
Maël
  • 45,206
  • 3
  • 29
  • 67