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,我想使用该功能对它们的笛卡尔乘积:
df1
和df2
是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
}
}
建议1 - 提供可重复的例子。 – Chase
我不幸没有,也不能找到任何。 – Anon
您的R会话中没有'df1'和'df2'?如果你没有数据来试验你的代码,你怎么知道你得到的错误?你可以添加'dput(df1)','dput(df2)',以及其他任何我们需要的数据来产生你的问题?或者给我们提供一部分数据......或者使用'sample','letters','rnorm'或者你的数据看起来应该是什么来产生一个足够代表性的例子。 – Chase