Mohammad A. Chaudhary
2005-Apr-13 18:28 UTC
[R] extracting one element of correlation matrices from a list poroduced by the 'by' statement
I am producing 2X2 correlation matrices by a class variable. I need to extract a vector of correlation coefficients only. I am doing that in a loop (see below) but I am sure there would be a simpler way. Please help!> by(d1[,c(2,3)],d1[,1],cor)d1[, 1]: 1 c e c 1.0000000 0.1972309 e 0.1972309 1.0000000 ------------------------------------------------------------------------ --------- d1[, 1]: 2 c e c 1.0000000 0.2469402 e 0.2469402 1.0000000 ------------------------------------------------------------------------ --------- d1[, 1]: 3 c e c 1.0000000 0.3177058 e 0.3177058 1.0000000 ------------------------------------------------------------------------ --------- d1[, 1]: 4 c e c 1.0000000 0.3492043 e 0.3492043 1.0000000 ------------------------------------------------------------------------ --------- d1[, 1]: 5 c e c 1.0000000 0.3385547 e 0.3385547 1.0000000 ------------------------------------------------------------------------ --------- d1[, 1]: 6 c e c 1.0000000 0.2876410 e 0.2876410 1.0000000 ------------------------------------------------------------------------ --------- d1[, 1]: 7 c e c 1.0000000 0.3374766 e 0.3374766 1.0000000 ------------------------------------------------------------------------ --------- d1[, 1]: 8 c e c 1.000000 0.438019 e 0.438019 1.000000 ------------------------------------------------------------------------ --------- d1[, 1]: 9 c e c 1.0000000 0.3452468 e 0.3452468 1.0000000 ------------------------------------------------------------------------ --------- d1[, 1]: 10 c e c 1.0000000 0.3746597 e 0.3746597 1.0000000 ***********************************************> t<- rep(0,10)> ind=0> for(r in 1:10) {+ ind=ind+1 + t[ind] <- by(d1[,c(2,3)],d1[,1],cor)[[r]][1,2] + }> t[1] 0.1972309 0.2469402 0.3177058 0.3492043 0.3385547 0.2876410 0.3374766 0.4380190 0.3452468 0.3746597 [[alternative HTML version deleted]]
Peter Dalgaard
2005-Apr-13 20:11 UTC
[R] extracting one element of correlation matrices from a list poroduced by the 'by' statement
"Mohammad A. Chaudhary" <mchaudha at jhsph.edu> writes:> I am producing 2X2 correlation matrices by a class variable. I need to > extract a vector of correlation coefficients only. I am doing that in a > loop (see below) but I am sure there would be a simpler way. Please > help! > > > > > by(d1[,c(2,3)],d1[,1],cor) >.....> c e > > c 1.0000000 0.3746597 > > e 0.3746597 1.0000000 > > *********************************************** > > > t<- rep(0,10) > > > ind=0 > > > for(r in 1:10) { > > + ind=ind+1 > > + t[ind] <- by(d1[,c(2,3)],d1[,1],cor)[[r]][1,2] > > + } > > > t > > [1] 0.1972309 0.2469402 0.3177058 0.3492043 0.3385547 0.2876410 > 0.3374766 0.4380190 0.3452468 0.3746597One 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. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Mohammad A. Chaudhary
2005-Apr-13 20:26 UTC
[R] extracting one element of correlation matrices from a list poroduced by the 'by' statement
Great! Thank you very much. Looks R has unlimited possibilities. Regards, Ashraf -----Original Message----- From: pd at pubhealth.ku.dk [mailto:pd at pubhealth.ku.dk] On Behalf Of Peter Dalgaard Sent: Wednesday, April 13, 2005 4:11 PM To: Mohammad A. Chaudhary Cc: r-help at stat.math.ethz.ch Subject: Re: [R] extracting one element of correlation matrices from a list poroduced by the 'by' statement "Mohammad A. Chaudhary" <mchaudha at jhsph.edu> writes:> I am producing 2X2 correlation matrices by a class variable. I need to > extract a vector of correlation coefficients only. I am doing that ina> loop (see below) but I am sure there would be a simpler way. Please > help! > > > > > by(d1[,c(2,3)],d1[,1],cor) >.....> c e > > c 1.0000000 0.3746597 > > e 0.3746597 1.0000000 > > *********************************************** > > > t<- rep(0,10) > > > ind=0 > > > for(r in 1:10) { > > + ind=ind+1 > > + t[ind] <- by(d1[,c(2,3)],d1[,1],cor)[[r]][1,2] > > + } > > > t > > [1] 0.1972309 0.2469402 0.3177058 0.3492043 0.3385547 0.2876410 > 0.3374766 0.4380190 0.3452468 0.3746597One 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. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907