简体   繁体   中英

Find numbers and letters in a vector

I have a vector composed by these strings:

10I/V/F/R, 16E, 20R/M/T/I, 24I, 32I, 33F/I/V, 36I/L/V,45R, 46I/L/V, 48M/V, 53L,
54L/M/V, 60E, 63P, 71V/T/I,73C/S/T/A, 82A/F/I/S/T/M/L/C, 84A/C/V,85V, 88D/T/S, 89M/V/Q/T, 90M

And I have another like that:

10F, 20M, 33F, 82A, 89Q, 93K

I would like to verify if each string of my second vector exists in the first. In this example, it is true for the first five, because for each number, the letter associated is present. I would like to receive as answer the numbers of strings that match, in this case, 5.

We can transform the search strings into regexes and then match each one against the target vector. Since you want the number of search strings that matched, we can count how many search strings had at least one match in the target vector.

sum(lengths(lapply(sub('^([0-9]+)([a-zA-Z])$','^\\1.*\\2',b),grep,a))>0L);
## [1] 5

Data

a <- c('10I/V/F/R','16E','20R/M/T/I','24I','32I','33F/I/V','36I/L/V,45R','46I/L/V','48M/V',
'53L','54L/M/V','60E','63P','71V/T/I,73C/S/T/A','82A/F/I/S/T/M/L/C','84A/C/V,85V','88D/T/S',
'89M/V/Q/T','90M');
b <- c('10F','20M','33F','82A','89Q','93K');

First we extract all digits in v1 and store them in l1 and we extract all alpha and store them in l2 . We then combine the two in lst using mapply() and paste0() . Finally, we loop over v2 and sum() all matches of grepl() returning TRUE

library(stringi)

l1  <- stri_extract_all_regex(v1, "[:digit:]+")
l2  <- stri_extract_all_regex(v1, "[:alpha:]")
lst <- mapply(function(x, y) paste0(x, y), l1, l2)

sum(sapply(v2, function(x) grepl(x, lst)))

Which gives:

#[1] 5

Data

v1 <- c("10I/V/F/R", "16E", "20R/M/T/I", "24I", "32I", "33F/I/V", 
        "36I/L/V", "45R", "46I/L/V", "48M/V", "53L", "54L/M/V", 
        "60E", "63P", "71V/T/I", "73C/S/T/A", "82A/F/I/S/T/M/L/C", 
        "84A/C/V", "85V", "88D/T/S", "89M/V/Q/T", "90M")

v2 <- c("10F", "20M", "33F", "82A", "89Q", "93K");

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