Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R intersection of lists

Tags:

r

I have 2 lists, each with multiple variables (I think this is the correct term). And I want to find the intersection for each variable. See the example below:

x<-list(A=c(1,2,3),B=c(4,5,6),C=c(7,8,9)) #input
y<-list(A=c(1,3,6,7),B=c(5,7,8),C=c(7,9,10)) #input
xinty<-list(A=c(1,3),B=5,C=c(7,8)) # desired output

I have tried the following, but it is obviously wrong. Any suggestions would be greatly appreciated. Thanks.

xinty<-lapply(x,function(x) intersect(x,y))
like image 481
user1249760 Avatar asked Oct 08 '22 09:10

user1249760


1 Answers

What about:

mapply(intersect, x,y)
like image 188
johannes Avatar answered Oct 12 '22 20:10

johannes