71
votes
$\begingroup$

How can I remove duplicate rows from this example data frame?

A   1
A   1
A   2
B   4  
B   1
B   1
C   2
C   2

I would like to remove the duplicates based on both the columns:

A   1
A   2
B   4
B   1
C   2

Order is not important.

$\endgroup$
2
  • $\begingroup$ @whuber shouldn't that be moved to SO? $\endgroup$
    – llrs
    Commented Mar 31, 2017 at 9:55
  • $\begingroup$ @Llopis Yes, but it's too late to do that now--and it was too late when we originally closed it. This kind of question was considered (borderline) on-topic many years ago but nowadays it would be migrated quickly. $\endgroup$
    – whuber
    Commented Mar 31, 2017 at 14:12

2 Answers 2

115
votes
$\begingroup$

unique() indeed answers your question, but another related and interesting function to achieve the same end is duplicated().

It gives you the possibility to look up which rows are duplicated.

a <- c(rep("A", 3), rep("B", 3), rep("C",2))
b <- c(1,1,2,4,1,1,2,2)
df <-data.frame(a,b)

duplicated(df)
[1] FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE

> df[duplicated(df), ]
  a b
2 A 1
6 B 1
8 C 2

> df[!duplicated(df), ]
  a b
1 A 1
3 A 2
4 B 4
5 B 1
7 C 2
$\endgroup$
1
  • 2
    $\begingroup$ Thanks for mentioning the 'duplicated' function. It can be used to delete duplicated rows based on a subset of the columns. $\endgroup$
    – Joko
    Commented Jan 20, 2016 at 15:27
51
votes
$\begingroup$

You are looking for unique().

a <- c(rep("A", 3), rep("B", 3), rep("C",2))
b <- c(1,1,2,4,1,1,2,2)
df <-data.frame(a,b)
unique(df)

> unique(df)
  a b
1 A 1
3 A 2
4 B 4
5 B 1
7 C 2
$\endgroup$
1
  • 1
    $\begingroup$ Thanks Bernd. I thought unique can be applied only for a specific column. I didn't know that it can be used for the entire data frame as well. thanks again $\endgroup$
    – Jana
    Commented Jan 31, 2011 at 20:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.