首页 > 解决方案 > 从字符串中提取单词

问题描述

我想提取唯一的对象 Scaffold 和每个字符串的脚手架数量(例如脚手架 6)。有任何想法吗?

 [2] "KQ415657.1 isolate UCB-ISO-001 unplaced genomic scaffold Scaffold5, whole genome shotgun sequence"   
   [3] "ABCD0100000.1  isolate UCB-ISO-001 Scaffold6_contig_1, whole genome shotgun sequence"                
   [4] "ABCDD0100001.1  isolate UCB-ISO-001 Scaffold8_contig_1, whole genome shotgun sequence"                
   [5] "ABCD0100002.1  isolate UCB-ISO-001 Scaffold2_contig_1, whole genome shotgun sequence"               
   [6] "ABCD0100003.1  isolate UCB-ISO-001 Scaffold6_contig_1, whole genome shotgun sequence"               
   [7] "ABCD0100004.1  isolate UCB-ISO-001 Scaffold2_contig_1, whole genome shotgun sequence"               
   [8] "ABCD0100005.1  isolate UCB-ISO-001 Scaffold7_contig_1, whole genome shotgun sequence"               
   [9] "ABCD0100006.1  isolate UCB-ISO-001 Scaffold8_contig_1, whole genome shotgun sequence"               

标签: rstring

解决方案


这是存储为字符串向量还是data.frame?每行是否总是包含一个 Scaffold 字符串?

如果它只是一个向量:

STRING = c("This is some vector Scaffold1", "some Scaffold20 string with stuff")

stringr::str_split(string = STRING, pattern = " ") %>% 
    lapply(function(x) x[grepl("Scaffold", x)]) %>% 
    unlist()
[1] "Scaffold1"  "Scaffold20"

如果您可以将它放在 data.frame 中,它可能会更整洁:

library(tidyverse)
data.frame(String = STRING, stringsAsFactors = F) %>% 
    separate(String, paste0("V", 1:8), remove = F) %>% 
    gather(key,val, starts_with("V")) %>% 
    filter(grepl("Scaffold", val)) %>% 
    select(-key)
                             String        val
1 some Scaffold20 string with stuff Scaffold20
2     This is some vector Scaffold1  Scaffold1

推荐阅读