# [R] Obtaining correlation parameters for multiple rows

jpnitya joao at genetics.med.harvard.edu
Fri Apr 25 22:03:46 CEST 2008

```Hi Jorge,

That was great! Thank you very much for the suggestion.

I just had to make a couple of minor changes (e.g., pearson vs. spearman)
and it worked perfectly.

Thanks,
Joao Pedro

Hi,

I'm sure it could be better but try this:

# F statistics based on lm
FSTAT=function(y,x) summary(lm(y~x))\$f[1]

# Correlation and p-value
CORR=function(y,x){
tc=cor.test(x,y,method="spearman",alternative="two.sided")
temp=matrix(c(tc\$estimate,tc\$p.value),ncol=2)
colnames(temp)=c('rho','pvalue')
temp
}

# 1000 variables and 100 samples
set.seed(124)
X=matrix(rnorm(1000*100),ncol=100)

# Correlation coefficient, p-value and F statistics
corr=t(apply(X[-1,],1,CORR,x=X[1,]))  # Your reference is X[1,]
fs=apply(X[-1,],1,FSTAT,x=X[1,])      # Your reference is X[1,]

# Report
temp=data.frame(fstats=fs,rho=corr[,1],pvalue=corr[,2])
rownames(temp)=paste("X",2:nrow(X),sep="")
dim(temp)
[1] 999   3

temp[1:10,]
fstats         rho     pvalue
X2  1.421307790 -0.05038104 0.61807912
X3  0.051423768 -0.04614461 0.64795111
X4  0.128000634  0.01795380 0.85902211
X5  0.990235820 -0.06540654 0.51730942
X6  5.569006085  0.24232823 0.01532172
X7  0.001862766 -0.01436544 0.88703532
X8  1.025363077 -0.10628263 0.29206908
X9  0.679794149  0.06509451 0.51927479
X10 1.296034903  0.09492949 0.34686211
X11 0.126636867  0.05137714 0.61110106

HTH,

Jorge

--
View this message in context: http://www.nabble.com/Obtaining-correlation-parameters-for-multiple-rows-tp16851980p16904683.html
Sent from the R help mailing list archive at Nabble.com.

```