Remove all rows of a category if one row meets a condition
dataframe, r, subset
Solution
We group by 'subj' and then `filter` based on the logical condition created with `any` and `!`
library(dplyr)
df1 %>%
group_by(subj) %>%
filter(!any(fixations==0 & ias == "A"))
# subj ias fixations
# <int> <chr> <int>
#1 1 A 17
#2 1 B 14
#3 3 A 15
#4 3 B 0
#5 4 A 8
#6 4 B 6
Or use `all` with `|`
df1 %>%
group_by(subj) %>%
filter(all(fixations!=0 | ias !="A"))
The same approach can be used with `ave` from `base R`
df1[with(df1, !ave(fixations==0 & ias =="A", subj, FUN = any)),]
data
df1 <- structure(list(subj = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), ias = c("A",
"B", "A", "B", "A", "B", "A", "B"), fixations = c(17L, 14L, 0L,
0L, 15L, 0L, 8L, 6L)), .Names = c("subj", "ias", "fixations"),
class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8"))
Problem
Problem: I want to remove all the rows of a specific category if one of the rows has a certain value in another column (similar to problems in the links below). However, the main difference is I would like it to only work if it matches a criteria in another column. Making a practice df ``` prac_df <- data_frame( subj = rep(1:4, each = 4), trial = rep(rep(1:4, each = 2), times = 2), ias = rep(c('A', 'B'), times = 8), fixations = c(17, 14, 0, 0, 15, 0, 8, 6, 3, 2, 3,3, 23, 2, 3,3) ) ``` So my data frame looks like this. ``` subj ias fixations 1 1 A 17 2 1 B 14 3 2 A 0 4 2 B 0 5 3 A 15 6 3 B 0 7 4 A 8 8 4 B 6 ``` And I want to remove all of subject 2 because it has a value of 0 for fixations column in a row that ias has a value of A. However I want to do this without removing subject 3, because even though there is a 0 it is in a row where the ias column has a value of B. My attempt so far. ``` new.df <- prac_df[with(prac_df, ave(prac_df$fixations != 0, subj, FUN = all)),] ``` However this is missing the part that will only get rid of it if it has the value A in the ias column. I've attempted various uses of & or if but I feel like there's likely a clever and clean way I just don't know of. My goal is to make a df like this. ``` subj ias fixations 1 1 A 17 2 1 B 14 3 3 A 15 4 3 B 0 5 4 A 8 6 4 B 6 ``` Thank you very much! Related questions: R: Remove rows from data frame based on values in several columns How to remove all rows belonging to a particular group when only one row fulfills the condition in R?