unique vs duplicate problem

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

unique vs duplicate problem

Nico902
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.

Reply | Threaded
Open this post in threaded view
|

Re: unique vs duplicate problem

jholtman
Here is one way of doing it -- you can create your own functions:

>  x <- c(1, 2, 3, 3)
>
> allDup <-
+ function (value)
+ {
+     duplicated(value) | duplicated(value, fromLast = TRUE)
+ }
>
> duped <- unique(x[allDup(x)])
> duped
[1] 3
>
> setdiff(unique(x), duped)
[1] 1 2
>
>


On Mon, Jul 9, 2012 at 12:42 PM, Nico902 <[hidden email]> wrote:

> 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.
>
> ______________________________________________
> [hidden email] 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.



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: unique vs duplicate problem

Rui Barradas
In reply to this post by Nico902
Hello,

Maybe this function.


fun <- function(x) x %in% x[duplicated(x)]

x <- c(1, 2, 3, 3)
fun(x)


Hope this helps,

Rui Barradas

Em 09-07-2012 17:42, Nico902 escreveu:

> 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: unique vs duplicate problem

Nico902
excellent!!! thanks a lot!!
Reply | Threaded
Open this post in threaded view
|

Re: unique vs duplicate problem

arun kirshna
In reply to this post by Nico902
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.







----- Original Message -----
From: Nico902 <[hidden email]>
To: [hidden email]
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.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: unique vs duplicate problem

Peter Ehlers
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 <[hidden email]>
> To: [hidden email]
> 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.
>
> ______________________________________________
> [hidden email] 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.
>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: unique vs duplicate problem

arun kirshna
Hi Peter,

Thanks for testing it and notifying.  I didn't test it to other situtations.

Probably this should give similar results as Rui's
#Test 1:

x<-c(1,2,3,3,3,4,4,5)
func1<-function(x) !is.na(match(x,x[duplicated(x)]))
func1(x)
#[1] FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE

x[!func1(x)]
#[1] 1 2 5


#Rui's function fun<-function(x) x%in%x[duplicated(x)]
 fun(x)
#[1] FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
 identical(func1(x),fun(x))
#[1] TRUE

#Test2:
x2<-c(1,2,-1,-1,2,3,4,-4,4,5,5,5,6,7)
 x2[!func1(x2)]
#[1]  1  3 -4  6  7

identical(func1(x2),fun(x2))
#[1] TRUE

A.K.







----- Original Message -----
From: Peter Ehlers <[hidden email]>
To: arun <[hidden email]>
Cc: Nico902 <[hidden email]>; R help <[hidden email]>
Sent: Monday, July 9, 2012 4:14 PM
Subject: Re: [R] unique vs duplicate problem

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 <[hidden email]>
> To: [hidden email]
> 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.
>
> ______________________________________________
> [hidden email] 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.
>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.