简体   繁体   中英

R-Partial string matches

I have

x <- c("DOL3", "AnnA", "CAT5", "Johna", "543QLARA", "Poro")
y <- c("3QLAR", "DOL5", "CAT5", "ANNA", "John", "Sam")

and I want a function so that provides matches as follows:

my_function(x, y)

[1] 1, 2, 3, 4, 5, NA

Details: I need to find partial string-matches that either start the same, or end the same, or have some same letters in the middle(for example, a default value of 3 consecutive matching letter). I also need the function to give a match despite using capital letters or not. Lastly, I need the function to search for a match for each element from x to amongst all the elements of y, not a 1 to 1 comparison of the elements.

If anyone is familiar with a way to approach this please help. Thank you.

I think I fount the answer.

my_function_2 <- function(x, y) {
    for (i in 1:length(x)) {
     print(which(grepl(substring(x[i],1 ,3 ), y, ignore.case = TRUE)))
    }
}

It's not elegant (x must include the smaller terms (ie "3QLAR" and not "543QLARA")) and it doesn't cover everything BUT it will have to do.

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