Dear sir, 

Thanks a lot for the solution.

It was such a simple solution, but people like me close their minds and don't 
think of data.frame as a whole and keep on thinking about vector elements only.

I also almost got the solution when I tried
qq = unique(pp)

(qq <- sub(" .*","", qq)) but this was giving me only first element of qq.

so I reversed the way I had defined paste command and saved it in some otehr 
name and again applied above command and I got the element. But I know that is 
not how good programs are written.

Regards

Vincy



--- On Sat, 3/12/11, Petr Savicky <savi...@praha1.ff.cuni.cz> wrote:

From: Petr Savicky <savi...@praha1.ff.cuni.cz>
Subject: Re: [R] Identifying unique pairs
To: r-help@r-project.org
Received: Saturday, March 12, 2011, 2:10 PM

On Sat, Mar 12, 2011 at 03:20:01AM -0800, Vincy Pyne wrote:
> Dear R helpers
> 
> Suppose I have a data frame as given below
> 
> mydat = data.frame(x = c(1,1,1, 2, 2, 2, 2, 2, 5, 5, 6), y = c(10, 10, 10, 8, 
> 8, 8, 7, 7, 2, 2, 4))
> 
[...]
> 
> unique(mydat$x) will give me 1, 2, 5, 6? i.e. 4 values and
> unique(mydat$y) will give me 10, 8, 7, 2, 4.
> 
> What I need is a data frame where I will get a vector (say) x_new as (1, 2, 
> 2, 5, 6) and corresponding y_new as (10, 8, 7, 2, 4). I need to use these two 
> vectors viz. x_new and y_new seperately for further processing. They may be 
> under same data frame say mydat_new but I should be able to access them as 
> mydat_new$x_new and similarly for y.
> 
> I tried following way.
> 
> pp = paste(mydat$x, mydat$y)
> 
> pp = > pp
> ?[1] "1 10" "1 10" "1 10" "2 8"? "2 8"? "2 8"? "2 7"? "2 7"? "5 2"? "5 2"? "6 
> 4" 
> 
> qq = unique(pp)
> 
> > qq
> [1] "1 10" "2 8"? "2 7"? "5 2"? "6 4" 

Hi.

If i understand you correctly, then the solution is easy, since function
unique() can handle also rows of a data frame. Is the following, what
you expect?

  unique(mydat)

     x  y
  1  1 10
  4  2  8
  7  2  7
  9  5  2
  11 6  4

Petr Savicky.

______________________________________________
R-help@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.



        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to