【问题标题】:How do I split multiple delimited strings in a column in to a row and delete the duplicates in that row of gene IDs?如何将一列中的多个分隔字符串拆分为一行并删除该行基因 ID 中的重复项?
【发布时间】:2023-01-04 01:16:14
【问题描述】:

这是我的数据框的样子......

我 |法斯塔标头 | | ------ | | Zm00001eb122880_P002;Zm00001eb122880_P003;Zm00001eb122880_P005;Zm00001eb336740_P002;Zm00001eb336740_P001| | Zm00001eb031730_P001;Zm00001eb136170_P001 | | Zm00001eb273230_P001;Zm00001eb273230_P002 |

我设法使用下面的代码将列中的分隔字符串变成一行

library(tidyr)
library(dplyr)
without_02473 %>% 
  mutate(`Fasta headers` = strsplit(as.character(`Fasta headers`), ";")) %>%   unnest(`Fasta headers`) 

这导致了以下

Fasta headers
Zm00001eb122880_P002
Zm00001eb122880_P003
Zm00001eb122880_P005
Zm00001eb336740_P002
Zm00001eb031730_P001

但是,我希望最终得到以下结果。 |法斯塔标头 | | ------ | | Zm00001eb122880 | | Zm00001eb336740 | | Zm00001eb031730|
| Zm00001eb273230|

我尝试使用组和过滤器,unnest(string_string_array),但我没有成功。 有人能帮我吗?

【问题讨论】:

    标签: r string duplicates


    【解决方案1】:

    我们可能会使用

    library(dplyr)
    library(tidyr)
    without_02473 %>% 
       separate_rows(`Fasta headers`, sep = ";") %>%
       mutate(`Fasta headers` = trimws(`Fasta headers`, whitespace = "_.*"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-07
      • 2017-11-01
      • 2023-01-30
      • 2021-10-18
      • 2018-07-29
      • 1970-01-01
      • 2018-06-09
      相关资源
      最近更新 更多