Re: [R] Identifying unique pairs

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Sat, 12 Mar 2011 03:45:53 -0800

Hi:

This problem came up the other day - see

Dennis

On Sat, Mar 12, 2011 at 3:20 AM, Vincy Pyne <vincy_pyne_at_yahoo.ca> 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))
>
>
> mydat
> x y
> 1 1 10
> 2 1 10
> 3 1 10
> 4 2 8
> 5 2 8
> 6 2 8
> 7 2 7
> 8 2 7
> 9 5 2
> 10 5 2
> 11 6 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"
>
> So I get the desired pairs, but I want each element of pair in two columns
> seperately as
>
> x_new y_new
>
> 1 10
> 2 8
> 2 7
> 5 2
> 6 4
>
> Kindly guide
>
> Vincy
>
>
>
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

[[alternative HTML version deleted]]

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 Sat 12 Mar 2011 - 11:49:27 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 Sat 12 Mar 2011 - 13:10:21 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.