简体   繁体   中英

How to match reverse order of two vectors in R

I have two vectors

key.vec <- c("apple:ball", "ball:cat","dog:ear")

vec2 <- c("ball:apple","cat:cat","cat:ball","ball:cat","apple:ball")

I now want to use key.vec to look for items in vec2 and get the items regardless of their order as separated by : .

So the result should be as shown below if I try something like this vec2[match(key.vec,vec2)] :

result:

"apple:ball", "ball:cat", "ball:apple","cat:ball"

This should work.

Splitting all the vectors by each character and then finding frequency of each character ( key.vec ). Next finding if the same frequency of each character is present in target vector ( vec2 ). This would help to handle both patterns.

vec2[lapply(strsplit(vec2, ""), table) %in% lapply(strsplit(key.vec, ""), table)]

#[1] "ball:apple" "cat:ball"   "ball:cat"   "apple:ball"

You could use sub as follows:

full.key.vec <- c(key.vec, sub("(.*):(.*)", "\\2:\\1", key.vec))
ind <- vec2 %in% full.key.vec
vec2[ind]

Or in one line:

vec2[vec2 %in% c(key.vec, sub("(.*):(.*)", "\\2:\\1", key.vec))]

Why?

\\\\1 and \\\\2 are so called Backreferences. They match the 1st/2nd regex group. So (.*):(.*) matches the string as follows: (apple):(ball) . So the \\\\2:\\\\1 becomes \\\\2=ball:\\\\1=apple . For more information see: http://www.regular-expressions.info/backref.html

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