Skip to content
Prev 306722 / 398506 Next

Transform pairwise observations into a table

HI David,

Thanks for your input.
Third and fourth solutions looks very interesting.? 

with(dat1,tapply(coef,list(ind1,ind2),function(x) x))
# also gave the same result.
# I tried with aggregate() and ddply(), but may be not as elegant solution as yours.
?
matrix(with(dat1,aggregate(coef,list(ind1,ind2),FUN=function(x) x)[,3]),nc=4)
library(plyr)
matrix(ddply(dat1,.(ind1,ind2),FUN=function(x) x$coef)[,3],nc=4)
#????? [,1]? [,2]? [,3] [,4]
#[1,] 1.000 0.250 0.125? 0.5
#[2,] 0.250 1.000 0.125? 0.5
#[3,] 0.125 0.125 1.000? 0.5
#[4,] 0.500 0.500 0.500? 1.0
A.K.





----- Original Message -----
From: David Winsemius <dwinsemius at comcast.net>
To: arun <smartpink111 at yahoo.com>
Cc: "Hadjixenofontos, Athena" <AHadjixenofontos at med.miami.edu>; R help <r-help at r-project.org>; Bert Gunter <gunter.berton at gene.com>
Sent: Monday, October 1, 2012 10:33 PM
Subject: Re: [R] Transform pairwise observations into a table
On Oct 1, 2012, at 2:30 PM, arun wrote:

            
That looks a tad ... well, ... complicated. So perhaps these base-only solutions with tapply might be more accessible: Some of them do border on the whimsical, I will admit:

with (dat1, tapply(coef, list(ind1,ind2), I))

with (dat1, tapply(coef, list(ind1,ind2), c))

with (dat1, tapply(coef, list(ind1,ind2), "^", 1))

with (dat1, tapply(coef, list(ind1,ind2), "+", 0))

It is a specific response to the request for a `table`-like function tha twouldallow the application of other functions. Cnage the `1` to `2` in the third instance and you get the tabulated squares. And do not forget the availability of `ftable` to flatten the output of `tapply` retunred values.