R- How to delete all rows of a category if one row satisfies the condition

My first question, thanks for any help you can offer.

Problem: I want to delete all rows of a certain category if one of the rows has a certain value in another column (similar to the problems in the links below). However, the main difference is that I would like it to work only if it meets the criteria in another column.

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 is as follows.

   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 delete the whole item 2, because it has a value of 0 for the commit column in the row that has the value A. However, I want to do this without removing topic 3, because although there is 0 it is in the row where the ias column matters B.

My attempt so far.

new.df <- prac_df[with(prac_df, ave(prac_df$fixations != 0, subj, FUN = all)),]

, , A ias. , , , , , .

- df .

   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

!

:

R:

, , R?

+4
1

"subj", filter , any !

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

all |

df1 %>%
   group_by(subj) %>%
   filter(all(fixations!=0 | ias !="A"))

ave base R

df1[with(df1, !ave(fixations==0 & ias =="A", subj, FUN = any)),]

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"))
+4

Source: https://habr.com/ru/post/1675954/


All Articles