[R] unique vs duplicate problem

Peter Ehlers ehlers at ucalgary.ca
Mon Jul 9 22:14:40 CEST 2012


On 2012-07-09 11:07, arun wrote:
> Hi,
> Try this:
> #Duplicated:
> x<-c(1:3,3)
> x==x[duplicated(x)]
> #[1] FALSE FALSE  TRUE  TRUE
> #Unique:
>   x[!x==x[duplicated(x)]]
> #[1] 1 2
>
>
> A.K.
>

Try the above approach with
   x <- c(1,2,3,3,3,4,4,5)

I think Rui's solution is preferable.

Peter Ehlers

>
>
>
>
>
>
> ----- Original Message -----
> From: Nico902 <descostes at ciml.univ-mrs.fr>
> To: r-help at r-project.org
> Cc:
> Sent: Monday, July 9, 2012 12:42 PM
> Subject: [R] unique vs duplicate problem
>
> Hi,
>
> Let say I have a numeric vector:   x <- c(1, 2, 3, 3).
>
> I want on one hand numbers which are not duplicated ie "1,2" and duplicated
> "3".
>
> so I did:
>
>> duplicated(x)
> FALSE FALSE FALSE TRUE
>> unique(x)
> 1 2 3
>
> which is not what I want. Is there a function in R to have the following
> result:
>
>> duplicated(x)
> FALSE FALSE TRUE TRUE
>> unique(x)
> 1 2
>
> I could do it by programming some loops but I reckon somebody programmed a
> function already.
>
> Cheers.
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/unique-vs-duplicate-problem-tp4635868.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list