Home > Software engineering >  Remove or slice rows by some factor condition
Remove or slice rows by some factor condition

Time:07-26

In my example:

# Data set
nest <- c(2,2,2,2,2,2,3,3,3,3,3,3)
ClassType  <- c("Class_0_1","Class_1_3","Class_3_9","Class_9_25","Class_25_50",
"Class_50","Class_0_1","Class_1_3","Class_3_9","Class_9_25","Class_25_50","Class_50")
avg <- c(19.0,19.0,19.0,19.0,19.0,19.0,0.776,0.776,0.776,12.4,22.3,36.0)
ds <- data.frame(nest,ClassType,avg)
str(ds)
# 'data.frame': 12 obs. of  3 variables:
#  $ nest     : num  2 2 2 2 2 2 3 3 3 3 ...
#  $ ClassType: chr  "Class_0_1" "Class_1_3" "Class_3_9" "Class_9_25" ...
#  $ avg      : num  19 19 19 19 19 19 0.776 0.776 0.776 12.4 ...

I'd like to remove rows with ClassType == "Class_50" if avg is equal in "Class_25_50" and "Class_50" by nest variable. My desirable output is:

#    nest   ClassType    avg
# 1     2   Class_0_1 19.000
# 2     2   Class_1_3 19.000
# 3     2   Class_3_9 19.000
# 4     2  Class_9_25 19.000
# 5     2 Class_25_50 19.000
# 6     3   Class_0_1  0.776
# 7     3   Class_1_3  0.776
# 8     3   Class_3_9  0.776
# 9    3  Class_9_25 12.400
# 10    3 Class_25_50 22.300
# 11    3    Class_50 36.000

Please, any help with it?

CodePudding user response:

We could do it this way:

  1. group by nest

  2. create a helper column with your conditions

  3. negate the filter adapted to the conditions

library(dplyr)

ds %>% 
  group_by(nest) %>% 
  mutate(x = ifelse(avg == avg[ClassType=="Class_25_50"] &
                                 avg == avg[ClassType=="Class_50"], 1, 0)) %>% 
  filter(!(ClassType=="Class_50" & x==1 )) %>% 
  select(-x)

    nest ClassType      avg
   <dbl> <chr>        <dbl>
 1     2 Class_0_1   19    
 2     2 Class_1_3   19    
 3     2 Class_3_9   19    
 4     2 Class_9_25  19    
 5     2 Class_25_50 19    
 6     3 Class_0_1    0.776
 7     3 Class_1_3    0.776
 8     3 Class_3_9    0.776
 9     3 Class_9_25  12.4  
10     3 Class_25_50 22.3  
11     3 Class_50    36    
  • Related