Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unique element of a named list

is there a way to return only unique elements of named list

l <- list(x = c(1, 2), x = c(1, 2), x = c(1, 3), y = c(1,3))
# with unique, it doesn't work. it drops names

I’d like the output to be

list(x = c(1, 2), x = c(1,3), y = c(1,3))

For now, using

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

l %>%
    tibble::enframe() %>%
    dplyr::distinct() %>%
    tibble::deframe()

but it is slow and requires a lot of calculation.

It seems that is close, but drops the second x item

# Partial solution on stack overflow
l[!duplicated(l)]
list(x = c(1,2), y = c(1,3)

Thanks for your advice

>Solution :

Use duplicated on the names too:

l[!(duplicated(l) & duplicated(names(l)))]

# $x
# [1] 1 2
# 
# $x
# [1] 1 3
# 
# $y
# [1] 1 3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading