# [R] list matching

Marc Schwartz marc_schwartz at comcast.net
Sat Oct 6 22:12:58 CEST 2007

```On Sat, 2007-10-06 at 19:49 +0000, Adrian Dusa wrote:
> Dear list,
>
> Given a list of elements like:
> aa <- list(one=c("o", "n", "e"),
>                tea=c("t", "e", "a"),
>                thre=c("t", "h", "r", "e"))
>
> Is there a function that returns the intersection between all?
> Both match() and intersect() only deal with two arguments, but sometimes I
> have more.
>
> Here, it should return "e" (or NA if none of the letters are common).
>
> I have a solution to apply %in% multiple times (here two times, first between
> the first two and then between the result and the third) but... perhaps there
> is a better and quicker way.
>

intersectList <- function(x)
{
res <- table(unlist(sapply(x, unique)))
names(res[res == length(x)])
}

In the first line, I use unique() to ensure that if the same letter
appears more than once in the same list element, it is not included in
the result set in error, such as in 'L' here:

L <- list(a = c("a", "b", "b"), b = c("d", "b", "a"), c = c("d", "a"))

> L
\$a
 "a" "b" "b"

\$b
 "d" "b" "a"

\$c
 "d" "a"

So:

> intersectList(aa)
 "e"

> intersectList(L)
 "a"

HTH,

Marc Schwartz

```
{"ft_lang_label":"__label__en","ft_lang_prob":0.8144809,"math_prob":0.95123416,"size":1276,"snap":"2021-31-2021-39","text_gpt3_token_len":417,"char_repetition_ratio":0.09827044,"word_repetition_ratio":0.0,"special_character_ratio":0.37539184,"punctuation_ratio":0.15523465,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98770463,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-30T21:25:34Z\",\"WARC-Record-ID\":\"<urn:uuid:7b9667ba-bdfe-4ba0-a133-b22a73495bbf>\",\"Content-Length\":\"4069\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5e38251b-5fa3-4e39-83b0-f79e5d0fd1ae>\",\"WARC-Concurrent-To\":\"<urn:uuid:ee134ba1-0d75-4713-9140-3c7a698481ea>\",\"WARC-IP-Address\":\"129.132.119.195\",\"WARC-Target-URI\":\"https://stat.ethz.ch/pipermail/r-help/2007-October/142530.html\",\"WARC-Payload-Digest\":\"sha1:U7PQBVABPMSUKGCUQEVF3UEHSH7CPORV\",\"WARC-Block-Digest\":\"sha1:GF6CNBQUCOMV2454FLNGQHSA3A76LP3V\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046153980.55_warc_CC-MAIN-20210730185206-20210730215206-00071.warc.gz\"}"}