Skip to content
Prev 67601 / 398506 Next

extracting one element of correlation matrices from a list poroduced by the 'by' statement

"Mohammad A. Chaudhary" <mchaudha at jhsph.edu> writes:
.....
One way could be 

 sapply(by(d1[2:3],d1[,1],cor),"[",1,2)

another is 

 by(d1[2:3],d1[,1],function(f)cor(f)[1,2])

or, (this possibility never occurred to me before)

 by(d1[2:3],d1[,1], with, cor(c,e))


You might want to wrap the last two in a c() construct but they
actually are vectors already, they just don't look it when print.by
has done its work.