简体   繁体   中英

Extract multiple instances of a pattern from a string in R

I have a character vector t as follows.

t <- c("GID456 SPK711", "GID456 GID667 VINK", "GID45345 DNP990 GID2345", 
    "GID895 GID895 K350")

I would like to extract all the strings starting with GID and followed by a sequence of digits.

This works, but does not retrieve multiple instances.

gsub(".*(GID\\d+).*", "\\1", t)
[1] "GID456"  "GID667"  "GID2345" "GID895" 

How to extract all the strings in this case? The desired output is as follows

out <- c("GID456", "GID456", "GID667", "GID45345", "GID2345", 
        "GID895", "GID895")

Here's an approach using a package I maintain qdapRegex (I prefer this or stringi/stringr) to base for consistency and ease of use. I also show a base approach. In any event I'd look at this more as an "extraction" problem than a subbing problem.

y <- c("GID456 SPK711", "GID456 GID667 VINK", "GID45345 DNP990 GID2345", 
    "GID895 GID895 K350")

library(qdapRegex)
unlist(ex_default(y, pattern = "GID\\d+"))

## [1] "GID456"   "GID456"   "GID667"   "GID45345" "GID2345"  "GID895"   "GID895" 

In base R:

unlist(regmatches(y, gregexpr("GID\\d+", y)))

Through gsub

> t <- c("GID456 SPK711", "GID456 GID667 VINK", "GID45345 DNP990 GID2345", 
+        "GID895 GID895 K350")
> unlist(strsplit(gsub("(GID\\d+)|.", "\\1 ", t), "\\s+"))
[1] "GID456"   "GID456"   "GID667"   "GID45345" "GID2345" 
[6] "GID895"   "GID895"

I have used str_split function from the stringr package

library(stringr)
word.list = str_split(t, '\\s+') 
new_list <- unlist(word.list)
new_list[grep("GID", new_list)]

I hope this helps.

I'm late to the party, but this tidyverse one-liner might be useful for someone.

With stringr + dplyr:

t <- c("GID456 SPK711", "GID456 GID667 VINK", "GID45345 DNP990 GID2345", "GID895 GID895 K350")
str_extract_all(t, regex("GID\\d+")) %>% unlist()

gives:

[1] "GID456" "GID456" "GID667" "GID45345" "GID2345" "GID895" "GID895"  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM