duplicates in multiple columns

耗尽温柔 提交于 2019-11-26 07:31:34

问题


I have a data frame like so

> df
  a  b c    d
1 1  2 A 1001
2 2  4 B 1002
3 3  6 B 1002
4 4  8 C 1003
5 5 10 D 1004
6 6 12 D 1004
7 7 13 E 1005
8 8 14 E 1006

I want to remove the rows where there are repeated values in column c AND column d. So in this example rows 2,3,5 and 6 would removed.

I have used this, which works:

df[!(df$c %in% df$c[duplicated(df$c)] & df$d %in% df$d[duplicated(df$d)]),]
>df
  a  b c    d
1 1  2 A 1001
4 4  8 C 1003
7 7 13 E 1005
8 8 14 E 1006

but it seems clunky and I can\'t help but think there is a better way. Any suggestions?

In case anyone wants to re-create the data-frame here is the dput:

df = structure(list(a = c(1, 2, 3, 4, 5, 6, 7, 8), b = c(2, 4, 6, 
8, 10, 12, 13, 14), c = structure(c(1L, 2L, 2L, 3L, 4L, 4L, 5L, 
5L), .Label = c(\"A\", \"B\", \"C\", \"D\", \"E\"), class = \"factor\"), 
    d = c(1001, 1002, 1002, 1003, 1004, 1004, 1005, 1006)), .Names = c(\"a\", 
\"b\", \"c\", \"d\"), row.names = c(NA, -8L), class = \"data.frame\")

回答1:


It works if you use duplicated twice:

df[!(duplicated(df[c("c","d")]) | duplicated(df[c("c","d")], fromLast = TRUE)), ]

  a  b c    d
1 1  2 A 1001
4 4  8 C 1003
7 7 13 E 1005
8 8 14 E 1006



回答2:


Make a new object with the 2 columns:

df_dups <- df[c("c", "d")]

Now apply it to your main df:

df[!duplicated(df_dups),]

Looks neater and easy to see/change columns that you are using.



来源:https://stackoverflow.com/questions/13742446/duplicates-in-multiple-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!