2011-07-06 192 views
1

我正在使用名为LD()的遗传包中的函数。为了简化它的功能,它基本上需要一个基因型列表(A/A,A/C,G/A等)并创建一个值列表(D,D',r等)。它看起来是这样的:使用for循环填充矩阵

a=LD(genotype1,genotype2) 

的结果看起来像:

Pairwise LD 
----------- 
        D  D'  Corr 
Estimates: 0.1419402 0.8110866 0.6029553 

       X^2  P-value N 
LD Test: 10.90665 0.0009581958 15 

我只需要从科尔价值,所以我用a$r呼吁它。

我有2个dataframes,我想使用该功能对它们的笛卡尔乘积:

df1df2是2个dataframes,每列(COL)表示基因型的列表。 我想用一个for循环填写矩阵:

df1=data.frame(c("A/A","C/C","A/A"),c("G/G","T/T","T/T")) 
df2=data.frame(c("A/T","C/T","C/C"),c("A/A","A/T","G/G")) 
q=1 # acts as a counter 
n=length(df1$col1) # All lists are the same length 
k=length(df2$col2) # These are to set the dimensions of the matrix 
r=n*k 

m=matrix(data=NA, nrow=r, ncol=3, byrow=TRUE, dimnames=list(NULL, c("c14","c19","Link"))) 

for(i in (1:n)) 
{ 
    for(j in (1:k)) 
    { 
    geno1=genotype(df2)[j] #genotype is a function that must be applied to the 
    geno2=genotype(df1)[i] #lists before the LD() function can be used 
    d=LD(geno1,geno2) 

    m=d$r #I only need the values from this section of the output 

    ld[q,]=c(names(df1),names(df2),m) #This is supposed to fill out the matrix 
             #I'm also not sure of how to do that part 
    q=q+1 #this is so that the above line fills in the next row with each iteration 
    } 
} 

当我运行它,我得到一个错误:

Error in dim(a1) <- a1.d : 
dims [product "some number"] do not match the length of object ["another number"] 

我期待一个3列和许多划船第一列是第一个基因型的名称(df1的列名称),第二列是第二个基因型的名称(df2的列名称),第三列的值是从LD()函数获得的值

有什么建议吗?谢谢!

更新应答: 我设法得到它:

q=1 # acts as a counter 
n=length(t1$rs.) 
k=length(t2$rs.) 
r=n*k 

ld=matrix(data=NA, nrow=r, ncol=3, byrow=TRUE, dimnames=list(NULL, c("c14","c19","Link"))) 

for(i in (1:n)) 
{ 
    for(j in (1:k)) 
    { 
    deq=LD(genotype(g1[,i]),genotype(g2[,j])) 
    m=deq$r 
    ld[q,]=c(i,j,m) 
    q=q+1 
    } 
} 
+0

建议1 - 提供可重复的例子。 – Chase

+0

我不幸没有,也不能找到任何。 – Anon

+1

您的R会话中没有'df1'和'df2'?如果你没有数据来试验你的代码,你怎么知道你得到的错误?你可以添加'dput(df1)','dput(df2)',以及其他任何我们需要的数据来产生你的问题?或者给我们提供一部分数据......或者使用'sample','letters','rnorm'或者你的数据看起来应该是什么来产生一个足够代表性的例子。 – Chase

回答

1

我理解你的工作的第一部分困难。你为什么要使用两个data.frames?我通常给每个人一行一行,每个标记一行,并计算所有可能的配对比较。 然而,让我们假设你使用LD封装估计LD(是的,他们说这是过时了,但它仍然是最好的!) 您可以进行如下操作:

#extract the correlation r from LD results 
tc<-LD.object$"r" 
#build a three columns matrix with all the pairwise combination of two markers 
pwm<-combn(row.names(tc),2) 
pwld<-matrix(NA,nrow=ncol(pwm),ncol=3) 
pwld[,1:2]<-pwm[1:2,] 
#Fill the matrix 
for(aaa in 1:nrow(pwld)) 
{ 
pwld[aaa,3]<-tc[pwld[aaa,1],pwld[aaa,2]] 
}