R语言mapply应用一例

需求:求data frame下两列有交集的元素个数,例如,m2和m3存储的都是以逗号分割的字符串,现在求m2和m3的交集, x1<-data.frame(m1=c(1,2,3),m2=c('f,a,b,c','edfc,avv,c','g,e,f,k') ,m3=c('--,c,b,d','--kr,edfc,avcv,ce','f,g,l,e,l,p') ,stringsAsFactors=F) 答案, x1$gg=mapply(function(x,y) length(intersect(strsplit(x,split=',')],strsplit(y,split=',')])), x1$m2, x1$m3)  

Read More