使用for循环填充矩阵

时间:2011-07-06 19:40:06

标签: r matrix for-loop ld

我正在使用名为LD()的遗传包中的函数。为了简化它的作用,它基本上采用基因型列表(A / A,A / C,G / A等)并创建值列表(D,D',r等)。它看起来像这样:

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

我只需要来自Corr的值,所以我会用a$r来调用它。

我有2个数据帧,我想在他们的笛卡尔积上使用该函数:

df1df2是2个数据框,每列(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
  }
}

1 个答案:

答案 0 :(得分:1)

我很难理解你工作的第一部分。为什么要使用两个data.frames?我通常提供一个data.frame,每个人一行,每个标记一行,LD计算所有可能的成对比较。 但是,让我们假设您使用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]]
}
相关问题