在列表中查找重复项,包括排列 [英] Finding duplicates in a list, including permutations

查看:138
本文介绍了在列表中查找重复项,包括排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确定一个列表是否包含任何重复元素,同时考虑排列等价。所有矢量都是相同的长度。

I would like to determine whether a list contains any duplicate elements, while considering permutations as equivalent. All vectors are of equal length.

完成此项目最有效的方法(最短运行时间)是什么?

What is the most efficient way (shortest running time) to accomplish this?

## SAMPLE DATA
a  <- c(1, 2, 3)
b  <- c(4, 5, 6)
a.same <- c(3, 1, 2)

## BOTH OF THSE LISTS SHOULD BE FLAGGED AS HAVING DUPLICATES
myList1 <- list(a, b, a)
myList2 <- list(a, b, a.same)


# CHECK FOR DUPLICATES
anyDuplicated(myList1) > 0  # TRUE
anyDuplicated(myList2) > 0  # FALSE, but would like true. 

现在我正在检查列表中的每个成员之前检查重复项

For now I am resorting to sorting each member of the list before checking for duplicates

anyDuplicated( lapply(myList2, sort) ) > 0

我想知道是否有更有效的选择。另外,在?复制的文档中,它表示使用这个列表可能很慢。还有其他功能更适合列表吗?

I am wondering if there is a more efficient alternative. Also, in the ?duplicated documentation, it indicates "Using this for lists is potentially slow". Are there other functions better suited for lists?

推荐答案

这个怎么办?

a  <- c(1, 2, 3)
b  <- c(4, 5, 6)
a.same <- c(3, 1, 2)
myList1 <- list(a, b, a)
myList2 <- list(a, b, a.same)

# For exact duplicated values: List1
DF1 <- do.call(rbind, myList1)  # From list to data.frame
ind1 <- apply(DF1, 2, duplicated) # logical matrix for duplicated values
DF1[ind1] # finding duplicated values  
[1] 1 2 3

# For permutations: List2
DF2 <- do.call(rbind, myList2)
ind2 <- apply(apply(DF2, 1, sort), 1, duplicated)
DF2[ind2] # duplicated values
[1] 3 1 2

这篇关于在列表中查找重复项,包括排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆