Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Removing duplicate elements in a vector [duplicate]

Tags:

r

filter

vector

I have a vector like this:

x = c(1,2,3,4,5,6,4,5,6,7)

> x
 [1] 1 2 3 4 5 6 4 5 6 7

I want to get rid of duplicates and get something like this:

> [1] 1 2 3 7 

My attempt

y = x[duplicated(x)]

> y
[1] 4 5 6

> x[x!=y]
[1] 1 2 3 7
Warning message:
In x != y : longer object length is not a multiple of shorter object length
> 

What am I doing wrong?

Is this error something I should worry about?

Is there another way to do this without getting an error?

like image 222
Zyferion Avatar asked May 23 '16 00:05

Zyferion


People also ask

How do I get rid of duplicate elements?

We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.

How do I subset duplicates in R?

We can find the rows with duplicated values in a particular column of an R data frame by using duplicated function inside the subset function. This will return only the duplicate rows based on the column we choose that means the first unique value will not be in the output.


1 Answers

Beware using consecutive numbers in your tests!

x <- c(1,2,3,4,5,6,4,5,6,7)
x1 <- c(-1, -1, 2, 8, 8, 15)

keep_singles <- function(v){
  v[!(v %in% v[duplicated(v)])] 
}

keep_singles(x)

[1] 1 2 3 7

keep_singles(x1)

[1]  2 15
like image 83
Hugh Avatar answered Sep 25 '22 11:09

Hugh