0

I have a long large matrix R, which dim is (6300000,5),

OriR
   CHR_A     BP_A      SNP_A CHR_B     BP_B         SNP_B            R
1     22 16057417 rs62224618    22 16287538 Affx-52233492 -0.002015300
2     22 16057417 rs62224618    22 16287557   rs200923174  0.004968350
3     22 16057417 rs62224618    22 16287585 Affx-80289661 -0.000626054
4     22 16057417 rs62224618    22 16287663 Affx-80289662 -0.000250182
5     22 16057417 rs62224618    22 16287779 Affx-52336937 -0.000623341
6     22 16057417 rs62224618    22 16449075   rs138257042 -0.002058500
7     22 16057417 rs62224618    22 16495833   rs116911124  0.377585000
8     22 16057417 rs62224618    22 16595552   rs117578132  0.042400600
9     22 16057417 rs62224618    22 16648658   rs117982183 -0.003302660
10    22 16057417 rs62224618    22 16870425      rs131533 -0.097989400
......

and I want to get a square matrix like this

RefR
              rs5747999  rs16981507   rs17432784  rs11703901    rs9306242    rs5748748     rs7287116
rs5747999   0.9998533079  0.22741272  0.061150898 -0.06786304  0.026769834 -0.026756354 -0.0009783071
rs16981507  0.2274127157  0.99985331 -0.062953304 -0.02377114  0.114857304 -0.013900927  0.0320166275
rs17432784  0.0611508982 -0.06295330  0.999853308 -0.01797451 -0.037863907 -0.023093981 -0.0102337236
rs11703901 -0.0678630438 -0.02377114 -0.017974515  0.99985331 -0.285761145 -0.100716341  0.1065594221
rs9306242   0.0267698336  0.11485730 -0.037863907 -0.28576115  0.999853308 -0.002337315  0.0948993753
rs5748748  -0.0267563540 -0.01390093 -0.023093981 -0.10071634 -0.002337315  0.999853308 -0.1285878515
rs7287116  -0.0009783071  0.03201663 -0.010233724  0.10655942  0.094899375 -0.128587852  0.9998533079
rs4819925   0.0412228273  0.01565093 -0.024076225 -0.13615624  0.005497719 -0.236229750 -0.2250363289
rs5992600  -0.0446008532 -0.01468979 -0.031567391  0.11624349 -0.016098126  0.656105723 -0.1809428819
rs5994130   0.0529235116 -0.03102122 -0.005370086 -0.06982177  0.104630869  0.087716348  0.1084759869

where the column names and row names are unique(c(SNP_A,SNP_B)), and the entries of matrix RefR is OriR$R, correspondingly.

I can only figure out how to do it with a for loop, but it's too slow. Does anyone know how to do it fastly?

lele
  • 47
  • 6
  • Please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Albin Jan 19 '22 at 13:45
  • It is hard to say without a reproducible example, but maybe the solution to this question could be relevant: https://stackoverflow.com/questions/9833052/r-convert-big-table-into-matrix-by-column-names – Sandwichnick Jan 19 '22 at 13:50
  • Thanks for that. The sparseMatrix function is worked, I will try to ask better questions and provide reproducible example next time. – lele Jan 19 '22 at 15:49

0 Answers0