Hi R experts,

I want to extract matching elements between column x and column y and here is my code:

data=data.frame(x=c('xdcff','dfghj','erbmp'),y=c('aaaa','dvbgg','tg'))

data$x=as.character(data$x)

data$y=as.character(data$y)

data$m=0

for (i in 1:nrow(data)) {

if (nchar(as.character(data$x[i]))>1) {

data$m[i]=paste(intersect(strsplit(data$x[i],split='')[[1]],strsplit(data$y[i],split='')[[1]]),collapse = '') }}

data$m is the result I want. Besides, the to-be-compared strings could be Chinese characters. So, the split function is needed.

The thing is I got 500 thousands rows and it took like forever to run the loop. I appreciate it if you could share other ways to do it fast.

Best,

Veda