In this type of data:
df <- data.frame(
id = 1:12,
Sequ = c(1,1,1,1,
3,3,3,
5,5,5,
6,6),
N_ipu = c(1,0,1,1,
1,4,1,
1,1,1,
5,1)
)
I need to remove those Sequ
ences of contiguous rows that contain in column N_ipu
values other than 0
and 1
. I've tried the below, with little success:
df %>%
group_by(Sequ) %>%
# filter(N_ipu %in% c(1,0))
# filter(if_all(N_ipu) %in% c(1,0))
# filter(if_all(N_ipu), ~all(.) %in% c(1,0))
Using dplyr
, how can I subset df
on those Sequ
as wholes that contain only 0
and 1
?
The desired output is this:
id Sequ N_ipu
1 1 1 1
2 2 1 0
3 3 1 1
4 4 1 1
8 8 5 1
9 9 5 1
10 10 5 1