[英]Removing elements from a list in R with duplicated subelements
I have a list in R with several attributes inside, say: 我在R中有一个列表,里面有几个属性,例如:
[[1]]
[[1]]$membership
[1] 1 1 1 2 2 2 1 3 3
[[2]]$csize
[1] 4 3 2
[[3]]$no
[1] 3
[[2]]
[[1]]$membership
[1] 1 1 2 2 2 3 1 3 3 4 4
[[2]]$csize
[1] 3 3 3 2
[[3]]$no
[1] 4
[[3]]
[[1]]$membership
[1] 1 2 2 2 2 3 1 3 4 4 4
[[2]]$csize
[1] 2 4 2 3
[[3]]$no
[1] 4
and so on. 等等。
However, some elements of the list have the same $no
. 但是,列表中的某些元素具有相同的
$no
。 For example, for element [[1]]
, I have [[1]][[3]]$no [1]3
; 例如,对于元素
[[1]]
,我有[[1]][[3]]$no [1]3
; for element [[2]]
, I have [[2]][[3]]$no [1] 4
; 对于元素
[[2]]
,我有[[2]][[3]]$no [1] 4
; for element [[3]]
, I have [[3]][[3]]$no [1] 4
; 对于元素
[[3]]
,我有[[3]][[3]]$no [1] 4
; for element [[4]]
, I have [[4]][[3]]$no [1] 5
. 对于元素
[[4]]
,我有[[4]][[3]]$no [1] 5
。
How can I keep all the elements of the list with different $no
? 如何将列表的所有元素都保留为
$no
? Thank you! 谢谢!
Maybe something like the following will do it. 也许像下面这样会做到的。
First, create a list with the same structure as yours. 首先,创建一个与您的结构相同的列表。
lst <- lapply(1:3, function(x) list(membership = 1 + x, csize = 2 + x, no = 12 + (x > 1)))
Now see which elements have duplicated no
vectors and subset it. 现在看到的元素有重复
no
载体和其子集。
inx <- !duplicated(sapply(lst, function(x) x$no))
lst[inx]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.