Yan - 8 months ago 34

R Question

I have a list of vectors (characters). For example:

`my_list <- list(c("a", "b", "c"),`

c("a", "b", "c", "d"),

c("e", "d"))

For the intersection of all these three vectors, I could use:

`Reduce(intersect, my_list)`

Then, what if I want to find the common element that appears "at least" a certain amount of times in the list? Such as:

`somefunction(my_list, time=2)`

`c("a", "b", "c", "d")`

Thanks.

Answer

We can convert this to a `data.table`

and do the group by action to get the elements

```
library(data.table)
setDT(stack(setNames(my_list, seq_along(my_list))))[,
if(uniqueN(ind)==2) values , values]$values
#[1] "a" "b" "c" "d"
```

A `base R`

option would be to `unlist`

the 'my_list', find the frequency count with the replicated sequence of 'my_list' using `table`

, get the column sums, check whether it is equal to 2 and use that index to subset the `names`

.

```
tblCount <- colSums(table(rep(seq_along(my_list), lengths(my_list)), unlist(my_list)))
names(tblCount)[tblCount==2]
#[1] "a" "b" "c" "d"
```