Re: [R] Create pairwise table from columns?

From: hadley wickham <h.wickham_at_gmail.com>
Date: Sat, 10 May 2008 07:53:20 -0500

On Fri, May 9, 2008 at 7:17 PM, knussear <knussear_at_mac.com> wrote:
>
> Hi Group
>
> I have a large data set of individual pairwise values (250,000 rows) that I
> need to reshape into a pairwise matrix for mantel tests of these values
> versus genetic distances.
>
> the data are currently in columns formatted like so
> AnimalA, AnimalB, Score
> A1, A2, S1
> A1, A3, S2
> A1, A4, S3
> A2, A3, S4
> A2, A4, S5
> A3, A4, S6
> ...,....,....
>
>
> I need the final matrix to be formatted as
> A1 A2 A3 A4
> A1 0 S1 S2 S3
> A2 S1 0 S4 S5
> A3 S2 S4 0 S6
> A4 S3 S4 S6 0

One approach would be to use the reshape package (http://had.co.nz/reshape) -

dfm <- melt(mydf, id = c("AnimalA", "AnimalB")) cast(dfm, AnimalA ~ AnimalB)

Hadley

-- 
http://had.co.nz/

______________________________________________
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 10 May 2008 - 12:55:40 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 10 May 2008 - 13:30:38 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