I have a dataset made up of several groups with consecutive, numbered bins (not necessarily the same number of bins in each group), and a boolean presence/absence value. I'd like to be able to generate some output which indicates groups in which there are non-consecutive "present" values.
A minimal reprex would be like:
x <- NULL
x$group <- c(rep("A",4),rep("B", 5), rep("C",4))
x$bin <- c(1,2,3,4,1,2,3,4,5,1,2,3,4)
x$status <- c("absent", "present", "absent", "present", "absent", "present", "present", "absent", "absent", "absent", "absent", "present", "present")
as.data.frame(x)
group bin status
1 A 1 absent
2 A 2 present
3 A 3 absent
4 A 4 present
5 B 1 absent
6 B 2 present
7 B 3 present
8 B 4 absent
9 B 5 absent
10 C 1 absent
11 C 2 absent
12 C 3 present
13 C 4 present
The output could be another column in the same dataframe with a flag,
group bin status flag
1 A 1 absent 1
2 A 2 present 1
3 A 3 absent 1
4 A 4 present 1
5 B 1 absent 0
6 B 2 present 0
7 B 3 present 0
8 B 4 absent 0
9 B 5 absent 0
10 C 1 absent 0
11 C 2 absent 0
12 C 3 present 0
13 C 4 present 0
a separate dataframe or matrix like:
group flag
1 A TRUE
2 B FALSE
3 C FALSE
or a list:
> flagged_groups
[1] "A"
I feel like just by writing this I've sorted out some of what I have to do to get there, but I'd love to hear your thoughts for a concise (and tidy) way to distill my data.