简体   繁体   English

如何从 R 中的“setequally”列表中删除重复元素?

[英]How to remove from a list 'setequally' duplicated elements in R?

Suppose I have some list li in R whose elements are vectors.假设我在 R 中有一些列表li ,其元素是向量。 For instance,例如,

li=list(a=c(2,3,5),b=c(77,119,81),c=c(9,11,13),d=c(5,2,3),e=c(80,45,16),f=c(16,17,19),g=c(13,9,11),h=c(22,13,58)) 

It can be seen that all objects in li are different as vectors .可以看出, li中的所有对象作为向量都不同。 Therefore,所以,

duplicated(li)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

Also, the command unique(li) will return the same list li .此外,命令unique(li)将返回相同的列表li But note that the elements a and d as well as c and g are equal as sets .但请注意,元素 a 和 d 以及 c 和 g与集合相等。 Namely,即,

setequal(li$a,li$d)
[1] TRUE

and

setequal(li$c,li$g)
[1] TRUE

Consequently, the element a is duplicated as set by the element d and the element c is duplicated as set by the element g.因此,元素 a 被复制元素 d 的设置,而元素 c 被复制元素 g 的设置 My question is: How to remove from a list such 'setequally' duplicated elements in R ?我的问题是:如何从列表中删除 R 中这样的“setequally”重复元素?

What about:关于什么:

li[apply(sapply(li, function(x) sapply(li, setequal, x)), 2, sum)==1]

$b
[1]  77 119  81

$e
[1] 80 45 16

$f
[1] 16 17 19

$h
[1] 22 13 58

? ?

We can get the unique sort ed elements and then remove all the set equal elements with duplicated我们可以得到unique sort ED元素,然后删除所有的set具有相同的元素duplicated

li1 <- lapply(li, function(x) sort(unique(x)))
i1 <- !(duplicated(li1)|duplicated(li1, fromLast = TRUE))
li[i1]
#$b
#[1]  77 119  81

#$e
#[1] 80 45 16

#$f
#[1] 16 17 19

#$h
#[1] 22 13 58

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM