I'm struggling on how can I keep on my dataframe only the elements that the difference of values in measure are minimal 2 for each group in id1. Here is a toy example:
test = data.frame(measure = c(1, 2, 3, 2, 2, 4, 0, 1),
id1 = c("A", "B", "A", "B", "C", "C", "A", "D"),
id2 = c("16", "16", "16", "16", "17", "17", "17", "18"))
# > test
# measure id1 id2
# 1 1 A 16
# 2 2 B 16
# 3 3 A 16
# 4 2 B 16
# 5 2 C 17
# 6 4 C 17
# 7 0 A 17
# 8 1 D 18
#as result, I'd like something like
#> res
# measure id1 id2
# 1 1 A 16
# 3 3 A 16
# 5 2 C 17
# 6 4 C 17
# 7 0 A 17
The id1 equal A and D where removed, since their max(value) - min(value) < 2.
Any hint on how can I apply this?