我有2個的數據幀(DF 1和2)具有以下結構:創建從數據幀不同的矩陣,其中R爲環
CHR SNP A1 A2 FAM CA1 CA2
1 rs3818361 A G 0.35 7 13
2 rs7561528 A G 0.2 4 16
2 rs6733839 T C 0.45 9 11
2 rs744373 G A 0.45 9 11
2 rs35349669 T C 0.35 7 13
...
它們具有7列和41行數(不計數colnames行)。與列CA1和CA2我必須建立2×2矩陣的數據幀中的每一個的,像這樣:
[CA1 from df1] [CA2 from df1] #first row CA1 and CA2 from df1
[CA1 from df2] [CA2 from df2] #first row CA1 and CA2 from df2
[CA1 from df1] [CA2 from df1] #second row CA1 and CA2 from df1
[CA1 from df2] [CA2 from df2] #second row CA1 and CA2 from df2
[CA1 from df1] [CA2 from df1] #third row CA1 and CA2 from df1
[CA1 from df2] [CA2 from df2] #third row CA1 and CA2 from df2
我考慮一個for循環(I在1:41),但我有麻煩來鞏固這個想法。
for (i in 1:41) {
do
datai <- c(df1[i,7],df1[i,8],df2[i,7],df2[i,8])
matrixi <- matrix(datai, nrow=2, ncol=2, byrow=TRUE)
}
我想讓循環生成41個矩陣。我在矩陣和數據(datai和matrixi)中的「i」意思是「我想要其中的41個」。有什麼想法嗎?
它的工作原理,謝謝! :) – melunuge92