# How to subset unique values from a list in R?

R ProgrammingServer Side ProgrammingProgramming

We know that a list in R can have multiple elements of different data types but they can be the same as well. Whether we have the same type of elements or different ones, we might want to subset the list with unique values, especially in situations where we believe that the values must be same. To do this, we can use unique function.

## Example

Consider the below list −

Live Demo

x1<-sample(1:10,50,replace=TRUE)
x2<-rpois(50,5)
x3<-rpois(50,10)
x4<-rbinom(50,10,0.7)
List1<-list(x1,x2,x3,x4)
List1

## Output

[[1]]
[1] 1 3 10 8 10 3 9 1 8 4 6 5 3 9 2 10 10 9 9 6 1 1 2 4 3
[26] 10 7 6 1 1 2 9 3 6 9 9 3 9 7 8 10 10 5 3 5 2 6 2 10 1
[[2]]
[1] 6 9 12 4 4 6 3 7 5 3 5 5 4 5 3 2 6 2 7 4 2 10 1 3 7
[26] 2 4 8 6 2 8 2 3 7 3 2 9 6 8 6 10 3 5 4 5 6 4 7 4 8
[[3]]
[1] 12 11 18 10 13 8 12 11 10 13 9 7 10 9 11 13 12 10 10 4 12 18 14 9 12
[26] 9 11 5 12 10 11 9 12 9 4 7 12 9 11 14 8 14 17 10 10 12 12 10 10 9
[[4]]
[1] 8 5 8 10 6 7 4 7 7 6 8 8 5 6 6 7 6 7 6 7 7 8 8 7 8
[26] 5 6 7 5 8 8 7 8 8 7 8 7 6 8 6 9 7 7 6 5 6 7 6 7 9

Finding the unique elements in the list List1 −

## Example

unique(unlist(List1,use.names=FALSE))

## Output

[1] 1 3 10 8 9 4 6 5 2 7 12 11 18 13 14 17

Let’s have a look at another example −

## Example

Live Demo

a<-rpois(50,2)
b<-sample(1:20,50,replace=TRUE)
c<-sample(1:50,100,replace=TRUE)
d<-rpois(100,2)
e<-round(runif(100,2,5),0)
f<-round(rnorm(25),0)
g<-sample(1:5,100,replace=TRUE)
h<-sample(5:10,100,replace=TRUE)
i<-rnorm(20)
j<-rpois(100,50)
List2<-list(a,b,c,d,e,f,g,h,i,j)
List2

## Output

[[1]]
[1] 5 3 2 3 2 3 0 2 1 2 0 4 1 3 2 2 2 1 4 2 2 4 2 4 2 4 2 2 3 4 2 0 1 1 2 1 3 2
[39] 0 2 3 1 4 2 0 2 0 1 0 3
[[2]]
[1] 10 19 10 15 4 14 4 9 20 1 12 13 4 12 4 5 15 6 7 18 9 6 1 7 4
[26] 19 13 5 20 6 16 4 17 7 9 8 1 17 2 16 14 8 9 10 14 10 1 18 4 11
[[3]]
[1] 12 24 45 8 41 13 11 35 17 43 8 21 36 18 2 31 3 44 17 22 41 36 48 18 21
[26] 12 22 39 45 14 17 36 41 19 27 17 21 49 12 27 21 47 14 37 44 21 38 46 40 42
[51] 41 45 41 1 48 37 34 14 1 13 41 47 4 43 26 36 9 45 47 31 33 9 14 47 46
[76] 47 33 11 27 6 28 31 24 17 36 10 23 16 14 6 18 29 37 6 11 42 38 15 20 29
[[4]]
[1] 0 1 1 2 4 8 4 2 2 2 1 1 1 4 0 2 1 0 4 2 2 1 1 3 4 2 5 2 1 1 2 5 1 3 0 2 1
[38] 3 3 1 2 3 0 1 0 1 5 4 1 4 0 3 4 0 0 5 2 0 6 1 7 3 3 3 1 6 0 0 1 2 1 2 2 3
[75] 3 2 3 0 2 2 1 1 0 4 3 4 2 1 1 1 1 2 0 1 4 0 1 5 1 1
[[5]]
[1] 5 2 4 5 5 3 4 4 4 5 4 4 4 2 4 3 4 2 2 3 3 3 5 4 5 3 4 4 3 4 5 4 2 3 3 4 3
[38] 4 5 3 2 2 2 3 4 3 4 4 5 2 2 3 3 3 3 4 2 4 3 3 4 3 3 5 3 2 4 4 3 3 3 3 5 3
[75] 2 3 3 3 5 3 4 5 4 3 3 4 3 3 3 2 3 4 2 4 4 2 5 5 3 4
[[6]]
[1] 0 -1 0 1 -1 0 1 1 0 1 0 1 1 0 -1 0 0 1 0 0 1 2 0 1 1
[[7]]
[1] 3 4 1 5 3 2 3 2 1 1 5 4 2 2 4 3 3 5 1 5 1 1 3 1 5 3 4 1 3 4 4 2 1 2 4 5 3
[38] 5 1 3 4 2 4 3 4 4 5 4 4 5 4 2 4 3 4 4 5 1 2 2 1 3 2 4 2 4 1 3 2 2 3 5 5 5
[75] 4 3 3 2 1 3 3 1 4 2 5 2 4 1 2 2 1 5 3 3 5 4 3 5 4 5
[[8]]
[1] 10 6 5 5 5 5 9 8 6 7 6 9 8 6 8 7 6 8 9 7 7 7 10 6 7
[26] 6 10 10 6 6 8 7 10 10 5 5 9 7 9 10 9 8 8 9 5 8 10 8 8 10
[51] 10 6 5 10 5 7 6 7 9 10 6 6 10 6 10 10 10 10 10 9 8 8 8 7 5
[76] 5 5 5 7 6 10 7 10 7 8 10 8 7 7 9 7 7 9 5 7 9 7 5 7 7
[[9]]
[1] -0.51557607 0.35591960 -0.41632268 -1.93573666 1.25093096 0.77053093
[7] 0.22564669 -1.15962836 -0.31592282 -0.81024610 -1.29726892 1.39639256
[13] -0.47600048 -0.15439720 -1.48831101 -0.71676607 0.87284911 -0.60246531
[19] -0.48863810 -0.09974965
[[10]]
[1] 57 50 42 60 59 55 43 46 46 42 39 65 51 52 46 56 43 38 56 45 56 32 50 48 59
[26] 44 42 50 36 56 51 43 48 56 50 42 44 47 39 40 46 52 33 56 49 50 46 50 54 52
[51] 51 45 39 64 51 41 54 56 57 47 59 47 43 54 60 58 61 57 46 39 50 53 65 48 50
[76] 55 47 48 55 42 54 45 42 59 45 45 48 54 54 53 45 48 53 37 60 46 45 52 48 45

## Example

unique(unlist(List2,use.names=FALSE))

## Output

[1] 5.00000000   3.00000000  2.00000000 0.00000000 1.00000000 4.00000000
[7] 10.00000000 19.00000000 15.00000000 14.00000000 9.00000000 20.00000000
[13] 12.00000000 13.00000000 6.00000000 7.00000000 18.00000000 16.00000000
[19] 17.00000000 8.00000000 11.00000000 24.00000000 45.00000000 41.00000000
[25] 35.00000000 43.00000000 21.00000000 36.00000000 31.00000000 44.00000000
[31] 22.00000000 48.00000000 39.00000000 27.00000000 49.00000000 47.00000000
[37] 37.00000000 38.00000000 46.00000000 40.00000000 42.00000000 34.00000000
[43] 26.00000000 33.00000000 28.00000000 23.00000000 29.00000000 -1.00000000
[49] -0.51557607 0.35591960 -0.41632268 -1.93573666 1.25093096 0.77053093
[55] 0.22564669 -1.15962836 -0.31592282 -0.81024610 -1.29726892 1.39639256
[61] -0.47600048 -0.15439720 -1.48831101 -0.71676607 0.87284911 -0.60246531
[67] -0.48863810 -0.09974965 57.00000000 50.00000000 60.00000000 59.00000000
[73] 55.00000000 65.00000000 51.00000000 52.00000000 56.00000000 32.00000000
[79] 54.00000000 64.00000000 58.00000000 61.00000000 53.00000000
Updated on 08-Sep-2020 13:29:48

Advertisements