如何根据 R 中该列中特定成员的值收集列的成员

How collect members of a column based on the value of a specific member in that column in R

在下面的数据框中,我想收集 B1 的成员,它们在 B2 中的值等于或大于 B2 中“b”的值。然后在这个新信息之后,统计每个B1成员出现了多少次。

数据框:

ID  B1  B2
z1  a   2.5
z1  b   1.7
z1  c   170
z1  c   9
z1  d   3
y2  a   0
y2  b   21
y2  c   15
y2  c   101
y2  d   30
y2  d   3
y2  d   15.5
x3  a   30.8
x3  a   54
x3  a   0
x3  b   30.8
x3  c   30.8
x3  d   7

所以结果是:

ID  B1  B2
z1  a   2.5
z1  c   170
z1  c   9
z1  d   3
y2  c   101
y2  d   30
x3  a   30.8
x3  a   54
x3  c   30.8

ID  B1  count
z1  a   1
z1  c   2
z1  d   1
y2  a   0
y2  c   1
y2  d   1
x3  a   2
x3  c   1
x3  d   0

使用 tidyverse:

library(tidyverse)

df %>% 
  group_by(ID) %>% 
  filter(B2 > B2[B1 == "b"]) %>%
  group_by(ID, B1) %>%
  count(name = "count") %>%
  as.data.frame()
#>   ID B1 count
#> 1 x3  a     1
#> 2 y2  c     1
#> 3 y2  d     1
#> 4 z1  a     1
#> 5 z1  c     2
#> 6 z1  d     1

reprex package (v2.0.1)

于 2022-04-26 创建

按 'ID'、filter 分组,其中 'B2' 大于或等于 'B2',其中 'B1' 也为 'b'创建另一个条件 'B1' 不等于 'b'

library(dplyr)
out1 <- df1 %>%
    group_by(ID) %>% 
    filter(any(B1 == "b") & B2 >= min(B2[B1 == "b"]), B1 != 'b') 

-输出

> out1
# A tibble: 9 × 3
# Groups:   ID [3]
  ID    B1       B2
  <chr> <chr> <dbl>
1 z1    a       2.5
2 z1    c     170  
3 z1    c       9  
4 z1    d       3  
5 y2    c     101  
6 y2    d      30  
7 x3    a      30.8
8 x3    a      54  
9 x3    c      30.8

第二个输出将使用 summarise 进行分组以获取行数,然后使用 complete

填充缺失的组合
library(tidyr)
out1 %>% 
  group_by(B1, .add = TRUE) %>%
  summarise(count = n(), .groups = "drop_last") %>% 
  complete(B1 = unique(.$B1), fill = list(count = 0)) %>%
  ungroup
# A tibble: 9 × 3
  ID    B1    count
  <chr> <chr> <int>
1 x3    a         2
2 x3    c         1
3 x3    d         0
4 y2    a         0
5 y2    c         1
6 y2    d         1
7 z1    a         1
8 z1    c         2
9 z1    d         1

数据

df1 <- structure(list(ID = c("z1", "z1", "z1", "z1", "z1", "y2", "y2", 
"y2", "y2", "y2", "y2", "y2", "x3", "x3", "x3", "x3", "x3", "x3"
), B1 = c("a", "b", "c", "c", "d", "a", "b", "c", "c", "d", "d", 
"d", "a", "a", "a", "b", "c", "d"), B2 = c(2.5, 1.7, 170, 9, 
3, 0, 21, 15, 101, 30, 3, 15.5, 30.8, 54, 0, 30.8, 30.8, 7)), 
class = "data.frame", row.names = c(NA, 
-18L))