R - 在列表条目中查找重复项

R - Finding duplicates in list entries

我想弄清楚如何从 R 中的列表对象中删除重复项。 所以我的示例列表:

examplelist <- list(a = c("blue", "red", "yellow"),
                    b = c("red", "black", "green"),
                    c = c("black", "green", "brown"))

我想得到的结果:

  1. 重复:c("red", "black", "green")
  2. 所有条目的向量,没有重复条目:c("blue", "red", "yellow", "black", "green", "brown")

除了 duplicated() 之外,我找不到其他函数,它只检查我的列表对象总数,而不是条目本身。

感谢您的帮助:)

你可以先unlist:

unlisted <- unlist(examplelist)
unlisted[duplicated(unlisted)]
#     b1      c1      c2 
#  "red" "black" "green" 

unlisted[!duplicated(unlisted)]
#      a1       a2       a3       b2       b3       c3 
#  "blue"    "red" "yellow"  "black"  "green"  "brown" 

如果您只需要向量(不包含名称),请使用 unname:

unlisted <- unname(unlist(examplelist))