Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove several rows based on one row

Tags:

r

dplyr

In the data.frame below, I have 3 variables. id (A1 to A10); var A to E and value. I want to remove specific rows based on these conditions

For each variable var (A, B, C, D or E);

If id == A5 and

if value < 5

then

remove all rows (10 rows) for this specific variable.

I will appreciate any suggestions how to do that.

> 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
))
like image 305
shiny Avatar asked Feb 04 '23 07:02

shiny


1 Answers

We can do this with dplyr using group_by and filter, we remove the group (var) which has any occurrence of id as "A5" and value less than 5.

library(dplyr)
df %>%
   group_by(var) %>%
   filter(!any(id == 'A5' & value < 5))
like image 107
Ronak Shah Avatar answered Feb 15 '23 13:02

Ronak Shah