Delete multiple lines based on one line

In the dataframe below, I have 3 variables. id (from A1 to A10); var A to E and value. I want to delete certain lines based on these conditions.

For each variable var

(A, B, C, D, or E);

If id == A5

u

if a value < 5

then

remove all lines (10 lines) for that particular variable.

I would be grateful for any suggestions on how to do this.

> dput(df)
structure(list(id = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 2L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 2L, 1L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 2L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 2L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 2L), .Label = c("A1", 
"A10", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9"), class = "factor"), 
    var = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("A", 
    "B", "C", "D", "E"), class = "factor"), value = c(2, 4, 7, 
    8, 8, 6, 3, 8, 4, 2, 3, 5, 4, 3, 2, 7, 4, 2, 1, 22, 54, 21, 
    36, 52, 14, 75, 12, 15, 24, 3, 2, 4, 5, 7, 9, 21, 22, 12, 
    12, 12, 0.3, 0.4, 0.9, 2, 3, 2, 0.5, 0.2, 0.4, 0.7)), .Names = c("id", 
"var", "value"), class = "data.frame", row.names = c(NA, -50L
))

      

+3


source to share


3 answers


We can do this with dplyr

using group_by

and filter

, we will remove the group ( var

), which has any

the appearance id

as the "A5" and value

less than 5.



library(dplyr)
df %>%
   group_by(var) %>%
   filter(!any(id == 'A5' & value < 5))

      

+5


source


We can do this with base R

df[with(df, ave(!(id == 'A5' & value < 5), var, FUN = any)),]

      




Or using data.table

library(data.table)
setDT(df)[df[, .I[any(!(id == 'A5' & value  < 5))], var]$V1]

      

+1


source


well you can always do

df %>% filter((id != A5) | (value >= 5))

      

0


source







All Articles