Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Extracting non-duplicated values from vector (not keeping one value for duplicates) [duplicate]

Tags:

r

I would like to keep the non-duplicated values from a vector, but without retaining one element from duplicated values. unique() does not work for this. Neither would duplicated().

For example:

> test <- c(1,1,2,3,4,4,4,5,6,6,7,8,9,9)
> unique(test)
[1] 1 2 3 4 5 6 7 8 9

Whereas I would like the result to be: 2,3,5,7,8

Any ideas on how to approach this? Thank you!

like image 297
arielle Avatar asked Mar 10 '17 16:03

arielle


1 Answers

You can use ave to count the length of sub-groups divided by unique values in test and retain only the ones whose length is 1 (the ones that have no duplicates)

test[ave(test, test, FUN = length) == 1]
#[1] 2 3 5 7 8

If test is comprised of characters, use seq_along as first argument of ave

test[ave(seq_along(test), test, FUN = length) == 1]
like image 151
d.b Avatar answered Oct 21 '22 09:10

d.b