Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a unique named list?

Tags:

r

This is a simple question and I think I can probably re-invent the wheel and write something custom but I'm sure there must be an easy way to do this that I can't think of at the moment. Suppose I have a list:

l <- list("NY"=10001, "CT"=10002, "CT"=10002)

I would like a list:

list("NY"=10001, "CT"=10002)

I tried to use unique(l) but it just returns:

list(10001, 10002)

How do I get a unique list but preserve the names assigned to the values?

like image 251
Denis Avatar asked Jan 26 '23 18:01

Denis


1 Answers

Using duplicated:

l[ !duplicated(l) ]
like image 178
zx8754 Avatar answered Jan 29 '23 07:01

zx8754