按部分字符串匹配分组

Group by partial string matches

我有一个 table,其中包含一个类别列表,每个类别都有一个计数值,我想根据相似性折叠这些计数值...例如 Mariner-1_Amel 和 Mariner-10 将是单一类别的 Mariner 和名称中带有 'Jockey' 或 'hAT' 的任何内容都应折叠。

我正在努力寻找能够应对所有可能性的解决方案。有简单的 dplyr 解决方案吗?

可重现

> dput(tibs)
structure(list(type = c("(TTAAG)n_1", "AMARI_1", "Copia-4_LH-I", 
"DNA", "DNA-1_CQ", "DNA/hAT-Charlie", "DNA/hAT-Tip100", "DNA/MULE-MuDR", 
"DNA/P", "DNA/PiggyBac", "DNA/TcMar-Mariner", "DNA/TcMar-Tc1", 
"DNA/TcMar-Tigger", "G3_DM", "Gypsy-10_CFl-I", "hAT-1_DAn", "hAT-16_SM", 
"hAT-N4_RPr", "HELITRON7_CB", "Jockey-1_DAn", "Jockey-1_DEl", 
"Jockey-12_DF", "Jockey-5_DTa", "Jockey-6_DYa", "Jockey-6_Hmel", 
"Jockey-7_HMM", "Jockey-8_Hmel", "LINE/Dong-R4", "LINE/I", "LINE/I-Jockey", 
"LINE/I-Nimb", "LINE/Jockey", "LINE/L1", "LINE/L2", "LINE/R1", 
"LINE/R2", "LINE/R2-NeSL", "LINE/Tad1", "LTR/Gypsy", "Mariner_CA", 
"Mariner-1_AMel", "Mariner-10_HSal", "Mariner-13_ACe", "Mariner-15_HSal", 
"Mariner-16_DAn", "Mariner-19_RPr", "Mariner-30_SM", "Mariner-39_SM", 
"Mariner-42_HSal", "Mariner-46_HSal", "Mariner-49_HSal", "TE-5_EL", 
"Unknown", "Utopia-1_Crp"), n = c(1L, 1L, 1L, 2L, 1L, 18L, 3L, 
9L, 2L, 8L, 21L, 12L, 18L, 1L, 3L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 
1L, 2L, 1L, 2L, 1L, 2L, 7L, 2L, 7L, 24L, 1L, 1L, 5L, 3L, 1L, 
1L, 7L, 1L, 5L, 1L, 1L, 5L, 5L, 1L, 1L, 3L, 5L, 5L, 2L, 1L, 190L, 
1L)), row.names = c(NA, -54L), class = c("tbl_df", "tbl", "data.frame"
))

如果没有共同点来定义组,您可以使用 case_when 定义单独的条件。

library(dplyr)
library(stringr)

tibs %>%
   mutate(category = case_when(str_detect(type, 'Mariner-\d+') ~ 'Mariner',  
                              str_detect(type, 'Jockey|hAT') ~ 'common', 
                              #Add more conditions
          ))

在我看来,您的更广泛的类型是 mostly/entirely 在字符串的开头。因此,您可以只使用该类型的第一个字母数字序列 ([[:alnum:]]+) 作为更广泛的类型。这将为您提供以下类型:

library(tidyverse)
df %>% 
  mutate(type_short = str_extract(type, "[[:alnum:]]+")) %>% 
  count(type_short, sort = TRUE)
#> # A tibble: 15 x 2
#>    type_short     n
#>    <chr>      <int>
#>  1 Mariner       12
#>  2 LINE          11
#>  3 DNA           10
#>  4 Jockey         8
#>  5 hAT            3
#>  6 AMARI          1
#>  7 Copia          1
#>  8 G3             1
#>  9 Gypsy          1
#> 10 HELITRON7      1
#> 11 LTR            1
#> 12 TE             1
#> 13 TTAAG          1
#> 14 Unknown        1
#> 15 Utopia         1

您可以轻松地使用新列来 group_by:

df %>% 
  mutate(type_short = str_extract(type, "[[:alnum:]]+")) %>% 
  group_by(type_short) %>% 
  summarise(n = sum(n))
#> # A tibble: 15 x 2
#>    type_short     n
#>    <chr>      <int>
#>  1 AMARI          1
#>  2 Copia          1
#>  3 DNA           94
#>  4 G3             1
#>  5 Gypsy          3
#>  6 hAT            5
#>  7 HELITRON7      1
#>  8 Jockey        10
#>  9 LINE          54
#> 10 LTR            7
#> 11 Mariner       35
#> 12 TE             1
#> 13 TTAAG          1
#> 14 Unknown      190
#> 15 Utopia         1

理论上,你也可以在这里尝试使用字符串相似性。然而你们的类型之间并没有很大的相似性。例如,相对 Levenshtein 距离(较长字符串的距离/字符)检索结果如下:

strings <- c("Mariner-1_Amel", "Mariner-10")
adist(strings) / max(nchar(strings))
#>           [,1]      [,2]
#> [1,] 0.0000000 0.3571429
#> [2,] 0.3571429 0.0000000

这可以解释为两种类型有 36% 的相似度。在那种情况下可能很难找到一个好的阈值。

此解决方案使用包 dplyr 函数 case_when 和基础 R grepl

library(dplyr)

tibs %>%
  mutate(category = case_when(
    grepl("hAT|Jockey", type) ~ "Jokey",
    grepl("Mariner", type) ~ "Mariner",
    grepl("DNA", type) ~ "DNA",
    grepl("LINE", type) ~"LINE",
    TRUE ~ as.character(type)
  ),
  category = factor(category)
  )