R-intersection lists

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 tried the following, but this is clearly wrong. Any suggestions would be appreciated. Thank.

xinty<-lapply(x,function(x) intersect(x,y))
+5
source share
1 answer

What about:

mapply(intersect, x,y)
+4
source

All Articles