2

Say I have data as follows

A <- c(1,1,1,2,2,2,3,3,3)
B <- c(1,0,0,1,0,0,1,0,0)
C <- c(8,7,6,8,7,8,9,9,11)

D <- data.frame(A,B,C)
D

library(dplyr)

E <- D %>%
  group_by(B) %>%
  filter(abs(diff(C)) <= 1)

to remove these cases, so that those shown in yellow are removed

enter image description here

in other words, for each a identity, when we assess the b=0 relative to b=1, any values where the c exceeds difference of 1.

eddi
  • 49,088
  • 6
  • 104
  • 155
lukeg
  • 1,327
  • 3
  • 10
  • 27

1 Answers1

5

Based on the description I think you want something like this:

D %>%
  group_by(A) %>%
  filter(abs(C - C[B == 1]) <= 1)
#Source: local data frame [7 x 3]
#Groups: A
#
#  A B C
#1 1 1 8
#2 1 0 7
#3 2 1 8
#4 2 0 7
#5 2 0 8
#6 3 1 9
#7 3 0 9
talat
  • 68,970
  • 21
  • 126
  • 157