How to split a list based on distinct element

I would like to split a list according to each distinct element

mylist <- list("first","first","second")

### What I would like
# A first list
list("first","first")
# A second list
list("second")

>Solution :

If you do the one-liner:

list2env(split(mylist, unlist(mylist)), globalenv())

Then you have the two lists in your global environment:

dput(first)
#> list("first", "first")

dput(second)
#> list("second")

Created on 2023-01-25 with reprex v2.0.2

Leave a Reply