Re: [R] selecting values that are unique, instead of selecting unique values

From: Gabor Csardi <csardi_at_rmki.kfki.hu>
Date: Wed, 25 Jun 2008 11:44:09 -0500

I'm sorry to say, but this one is wrong, too. Maybe coffee really helps, I just had one. :)

> Vec <- c(20:30,20)
> which(table(Vec) == 1)
21 22 23 24 25 26 27 28 29 30
 2 3 4 5 6 7 8 9 10 11

You would actually need the names, but that would involve some numberic -> character -> numeric conversion. My previous solution is better in this respect.

Btw. to get rid of names 'unname' is better, the code is more readable.

Best,
Gabor

On Wed, Jun 25, 2008 at 11:37:12AM -0500, Marc Schwartz wrote:
> on 06/25/2008 11:29 AM Marc Schwartz wrote:

>> on 06/25/2008 11:19 AM Daren Tan wrote:
>>>
>>> unique(c(1:10,1)) gives 1:10 (i.e. unique values), is there any
>>> method to get only 2:10 (i.e. values that are unique) ?
>>>
>>
>> The easiest might be:
>>
>>  > Vec
>>  [1]  1  2  3  4  5  6  7  8  9 10  1
>>
>>  > Vec[table(Vec) == 1]
>> [1]  2  3  4  5  6  7  8  9 10
>>
>

> Correction!
>

> That should be:
>

> > which(table(Vec) == 1)
> 2 3 4 5 6 7 8 9 10
> 2 3 4 5 6 7 8 9 10
>

> or if you don't want a named vector:
>

> > as.vector(which(table(Vec) == 1))
> [1] 2 3 4 5 6 7 8 9 10
>
>

> My initial solution was incorrect and only worked because of the
> ordering of the example vector.
>

> Time for another cup of cawfee...Oy
>

> Marc
>

> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-- 
Csardi Gabor <csardi_at_rmki.kfki.hu>    UNIL DGM

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 25 Jun 2008 - 16:51:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 25 Jun 2008 - 17:31:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive