0

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 Sequences of contiguous rows that contain in column N_ipuvalues 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 0and 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
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34

2 Answers2

1

Use any to remove all element of the group, and ! to invert the selection.

df %>% 
  group_by(Sequ) %>% 
  filter(!any(N_ipu > 1))

# A tibble: 7 x 3
# Groups:   Sequ [2]
     id  Sequ N_ipu
  <int> <dbl> <dbl>
1     1     1     1
2     2     1     0
3     3     1     1
4     4     1     1
5     8     5     1
6     9     5     1
7    10     5     1
Maël
  • 45,206
  • 3
  • 29
  • 67
1

You were very close

df %>%
  group_by(Sequ) %>%
   filter(all(N_ipu %in% c(1,0)))

     id  Sequ N_ipu
  <int> <dbl> <dbl>
1     1     1     1
2     2     1     0
3     3     1     1
4     4     1     1
5     8     5     1
6     9     5     1
7    10     5     1
Quixotic22
  • 2,894
  • 1
  • 6
  • 14