Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding unique values from a list

Tags:

list

r

unique

Suppose you have a list of values

x <- list(a=c(1,2,3), b = c(2,3,4), c=c(4,5,6)) 

I would like to find unique values from all list elements combined. So far, the following code did the trick

unique(unlist(x)) 

Does anyone know a more efficient way? I have a hefty list with a lot of values and would appreciate any speed-up.

like image 201
Roman Luštrik Avatar asked Oct 07 '10 07:10

Roman Luštrik


People also ask

How do I extract unique values from a list?

In Excel, there are several ways to filter for unique values—or remove duplicate values: To filter for unique values, click Data > Sort & Filter > Advanced. To remove duplicate values, click Data > Data Tools > Remove Duplicates.


1 Answers

This solution suggested by Marek is the best answer to the original Q. See below for a discussion of other approaches and why Marek's is the most useful.

> unique(unlist(x, use.names = FALSE)) [1] 1 2 3 4 5 6 

Discussion

A faster solution is to compute unique() on the components of your x first and then do a final unique() on those results. This will only work if the components of the list have the same number of unique values, as they do in both examples below. E.g.:

First your version, then my double unique approach:

> unique(unlist(x)) [1] 1 2 3 4 5 6 > unique.default(sapply(x, unique)) [1] 1 2 3 4 5 6 

We have to call unique.default as there is a matrix method for unique that keeps one margin fixed; this is fine as a matrix can be treated as a vector.

Marek, in the comments to this answer, notes that the slow speed of the unlist approach is potentially due to the names on the list. Marek's solution is to make use of the use.names argument to unlist, which if used, results in a faster solution than the double unique version above. For the simple x of Roman's post we get

> unique(unlist(x, use.names = FALSE)) [1] 1 2 3 4 5 6 

Marek's solution will work even when the number of unique elements differs between components.

Here is a larger example with some timings of all three methods:

## Create a large list (1000 components of length 100 each) DF <- as.list(data.frame(matrix(sample(1:10, 1000*1000, replace = TRUE),                                  ncol = 1000))) 

Here are results for the two approaches using DF:

> ## Do the three approaches give the same result: > all.equal(unique.default(sapply(DF, unique)), unique(unlist(DF))) [1] TRUE > all.equal(unique(unlist(DF, use.names = FALSE)), unique(unlist(DF))) [1] TRUE > ## Timing Roman's original: > system.time(replicate(10, unique(unlist(DF))))    user  system elapsed    12.884   0.077  12.966 > ## Timing double unique version: > system.time(replicate(10, unique.default(sapply(DF, unique))))    user  system elapsed    0.648   0.000   0.653 > ## timing of Marek's solution: > system.time(replicate(10, unique(unlist(DF, use.names = FALSE))))    user  system elapsed    0.510   0.000   0.512 

Which shows that the double unique is a lot quicker to applying unique() to the individual components and then unique() those smaller sets of unique values, but this speed-up is purely due to the names on the list DF. If we tell unlist to not use the names, Marek's solution is marginally quicker than the double unique for this problem. As Marek's solution is using the correct tool properly, and it is quicker than the work-around, it is the preferred solution.

The big gotcha with the double unique approach is that it will only work if, as in the two examples here, each component of the input list (DF or x) has the same number of unique values. In such cases sapply simplifies the result to a matrix which allows us to apply unique.default. If the components of the input list have differing numbers of unique values, the double unique solution will fail.

like image 165
Gavin Simpson Avatar answered Sep 19 '22 12:09

Gavin Simpson