在 R 中,如何根据该列中特定成员的值收集该列的其他成员?

In R, How collect other members of a column based on the value of a specific member in that column?

在下面的数据框中,我想收集 B1 的成员,它们在 B2 中的值等于或大于 B2 中“a”的值。

ID  B1  B2
z1  a   2.5
z1  b   1.7
z1  c   170
z1  d   3
y2  a   0
y2  b   0
y2  c   101
y2  d   -30
y3  a   30.8
x3  b   1
x3  c   30.8
x3  d   70.1

所以结果将是:

ID  B1  B2
z1  c   170
z1  d   3
y2  b   0
y2  c   101
x3  c   30.8
x3  d   70.1

然后统计B1成员(b,c,d)各出现了多少次

df %>%
   group_by(grp=cumsum(B1=='a'))%>%
   filter(B2>=first(B2), B1 != 'a')

# A tibble: 6 x 4
# Groups:   grp [3]
  ID    B1       B2   grp
  <chr> <chr> <dbl> <int>
1 z1    c     170       1
2 z1    d       3       1
3 y2    b       0       2
4 y2    c     101       2
5 x3    c      30.8     3
6 x3    d      70.1     3