Remove duplicated elements in R

Get unique elements of a vector

> v = c("a", "a", "b", "c", "c")
> unique(v)
[1] "a" "b" "c"

Get duplicated elements of a vector

> duplicated(v)
[1] FALSE  TRUE FALSE FALSE  TRUE
> v[duplicated(v)]
[1] "a" "c"

Get the non duplicated elements of a vector

> v[!duplicated(v)]
[1] "a" "b" "c"

Get duplicated elements of a dataframe

> df <- data.frame(c1 = c(rep("A", 3), rep("B", 3), rep("C", 2)), c2 = 1:8)
> df[duplicated(df$c1),]
  c1 c2
2  A  2
3  A  3
5  B  5
6  B  6
8  C  8

Get the non duplicated elements of a dataframe

We will get the first occurrence of the element in the dataframe

> df[!duplicated(df$c1),]
  c1 c2
1  A  1
4  B  4
7  C  7
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: