[英]How collect members of a column based on the value of a specific member in that column in 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.