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))
What about:
mapply(intersect, x,y)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With