{"id":210,"date":"2018-08-23T13:29:34","date_gmt":"2018-08-23T11:29:34","guid":{"rendered":"http:\/\/candrea.ch\/blog\/?p=210"},"modified":"2018-08-23T13:30:30","modified_gmt":"2018-08-23T11:30:30","slug":"recursive-r-function-to-find-intersection-of-two-or-more-given-sets","status":"publish","type":"post","link":"https:\/\/candrea.ch\/blog\/recursive-r-function-to-find-intersection-of-two-or-more-given-sets\/","title":{"rendered":"Recursive R function to find intersection of two or more given sets"},"content":{"rendered":"<p>The <span class=\"lang:default decode:true crayon-inline \">intersect(x, y)<\/span> function from R &#8220;base&#8221; package works for no more then two vectors x and y.\u00a0 The following function accepts as many vectors as you like and returns the intersection vector of them all:<\/p>\n<pre class=\"lang:r decode:true \">## recursive intersect function\r\nintersect.rec &lt;- function(x, ...) {\r\n    a &lt;- list(...)\r\n    if(length(a) == 0) stop(\"Two or more arguments are needed.\")\r\n    if(is.list(a[[1]])) a &lt;- a[[1]]\r\n    if(length(a) == 1) return(intersect(x, a[[1]]))\r\n    else intersect.rec(intersect(x, a[[1]]), a[-1])\r\n}<\/pre>\n<p>Sample usage:<\/p>\n<pre class=\"lang:r decode:true \">x &lt;- letters[1:5]\r\ny &lt;- letters[2:5]\r\nz &lt;- letters[3:5]\r\nintersect.rec(x, y, z)\r\n## returns\r\n## [1] \"c\" \"d\" \"e\"\r\n<\/pre>\n<p>This is especially a basic example of an R function using dots argument and recursively iterating over them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The intersect(x, y) function from R &#8220;base&#8221; package works for no more then two vectors x and y.\u00a0 The following function accepts as many vectors as you like and returns the intersection vector of them all: ## recursive intersect function intersect.rec &lt;- function(x, &#8230;) { a &lt;- list(&#8230;) if(length(a) == 0) stop(&#8220;Two or more arguments &hellip; <a href=\"https:\/\/candrea.ch\/blog\/recursive-r-function-to-find-intersection-of-two-or-more-given-sets\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Recursive R function to find intersection of two or more given sets<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-210","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/posts\/210"}],"collection":[{"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/comments?post=210"}],"version-history":[{"count":2,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":212,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/posts\/210\/revisions\/212"}],"wp:attachment":[{"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/media?parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/categories?post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/tags?post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}