Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all rows of a category if one row meets a condition [duplicate]

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?

like image 560
Kirk Geier Avatar asked Apr 28 '17 21:04

Kirk Geier


People also ask

How to delete all rows with a certain Value excel?

Press Ctrl + A to select all of them. You can select specific values you want to remove by using Ctrl or Shift keys. Close the Find and Replace window. Click OK button to delete those rows.

How do I delete rows in pandas DataFrame based on condition?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).

How do I drop a row based on conditions?

Delete rows based on the condition of a column We will use vectorization to filter out such rows from the dataset which satisfy the applied condition. Let's use the vectorization operation to filter out all those rows which satisfy the given condition.

How to delete multiple rows in excel with same Value?

Go ahead to right click selected cells and select the Delete from the right-clicking menu. And then check the Entire row option in the popping up Delete dialog box, and click the OK button. Now you will see all the cells containing the certain value are removed.


1 Answers

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"))
like image 176
akrun Avatar answered Sep 20 '22 18:09

akrun