0

我正在寻求有关如何从一个特定的成对比较矩阵(一个 210x4 矩阵到两个 21x21 双矩阵)中制作两个矩阵的帮助。

我有一系列人口,并且已经对两个变量进行了所有可能的成对比较。我正在寻找包含两个比较结果的两个矩阵,其中人口名称以特定顺序列出。

我的数据如下:

 Pop1      Pop2      MSa      MSw
 pop314      pop1001      0.142773893      0.413771712
 pop314      pop311      0.722622378      0.771592129
 pop314      pop803      1.208258657      0.457184787
 pop314      popLaVall      0.278680371      0.426460114
 pop314      pop313      1.501337793      0.57032967
 pop314      pop195      4.736120401      0.627472527
 pop314      pop317      21.85550936      0.346760222
 pop314      pop1031      6.335913362      0.500113647
 pop314      pop252      1.802985027      0.559383559
 pop314      pop254a      0.431497976      0.678803419
 pop314      pop1026      0.076486014      0.600192308
 pop314      popClapier      5.76474359      0.467032967
 pop314      pop315      0.957616397      0.492037787
 pop314      pop316      1.295657568      0.560735418
 pop314      pop925      0.003205128      0.64475052
 pop314      pop310      5.157311157      0.583150183
 pop314      pop1313      1.811687687      0.711513323
 pop314      pop145      2.831497976      0.612136752
 pop314      pop143      2.716180371      0.632478632
 pop314      popFreaux      2.12350363      0.78049021
 pop1001      pop311      2.401578947      0.699247312
 pop1001      pop803      0.604514089      0.407975911
 pop1001      popLaVall      0.034722222      0.299632353
 pop1001      pop313      2.816666667      0.380357143
 pop1001      pop195      7.35      0.423214286
 pop1001      pop317      34.99822191      0.278501229
 pop1001      pop1031      12.73486025      0.456225296
 pop1001      pop252      4.48675655      0.494660004
 pop1001      pop254a      0.09      0.53744186
 pop1001      pop1026      0.729166667      0.53277439
 pop1001      popClapier      5.168918919      0.335714286
 pop1001      pop315      2.309379217      0.363086771
 pop1001      pop316      3.61      0.507142857
 pop1001      pop925      0.150501672      0.512019231
 pop1001      pop310      9.166666667      0.454365079
 pop1001      pop1313      4.142156863      0.590214932
 pop1001      pop145      5.601111111      0.481627907
 pop1001      pop143      5      0.463235294
 pop1001      popFreaux      5.222979296      0.713258977
 pop311      pop803      12.78856521      0.619481522
 pop311      popLaVall      2.700082418      0.715926966
 pop311      pop313      0.596470588      0.773253012
 pop311      pop195      3.843529412      0.787710843
 pop311      pop317      38.83100386      0.625886978
 pop311      pop1031      9.937912002      0.62857444
 pop311      pop252      0.808336784      0.68821662
 pop311      pop254a      4.32      0.782040816
 pop311      pop1026      1.012715827      0.709616788
 pop311      popClapier      18.01076087      0.725333333
 pop311      pop315      0.142172452      0.727871854
 pop311      pop316      0.279677419      0.674705882
 pop311      pop925      1.456005331      0.768271173
 pop311      pop310      5.043232323      0.748591065
 pop311      pop1313      0.787997841      0.787113799
 pop311      pop145      1.92      0.75755102
 pop311      pop143      1.709010989      0.778426966
 pop311      popFreaux      1.266286411      0.804930491
 pop803      popLaVall      0.234271772      0.411605498
 pop803      pop313      6.304311454      0.447363217
 pop803      pop195      13.67574003      0.461997363
 pop803      pop317      83.31531532      0.379370196
 pop803      pop1031      47.51637337      0.464747356
 pop803      pop252      18.61408437      0.489638845
 pop803      pop254a      0.20975157      0.506636946
 pop803      pop1026      5.901904622      0.512656498
 pop803      popClapier      4.243688744      0.424536896
 pop803      pop315      7.048134722      0.433714223
 pop803      pop316      17.30517726      0.498248578
 pop803      pop925      1.864870062      0.49553704
 pop803      pop310      22.43390329      0.46996997
 pop803      pop1313      13.79355826      0.535141134
 pop803      pop145      15.3370243      0.481894678
 pop803      pop143      11.77177177      0.474815725
 pop803      popFreaux      22.90795131      0.632728648
 popLaVall      pop313      3.124038462      0.3890625
 popLaVall      pop195      7.616346154      0.4390625
 popLaVall      pop317      32.44776262      0.274609168
 popLaVall      pop1031      12.11845813      0.46105927
 popLaVall      pop252      4.688077657      0.503311173
 popLaVall      pop254a      0.007378049      0.558910256
 popLaVall      pop1026      1.0125      0.543269231
 popLaVall      popClapier      3.895833333      0.336693548
 popLaVall      pop315      2.621146617      0.367324561
 popLaVall      pop316      3.852083333      0.514760638
 popLaVall      pop925      0.313415751      0.530408654
 popLaVall      pop310      9.204166667      0.467653509
 popLaVall      pop1313      4.401323529      0.61205576
 popLaVall      pop145      5.822012195      0.497371795
 popLaVall      pop143      5.28125      0.48125
 popLaVall      popFreaux      5.354151454      0.72914079
 pop313      pop195      0.8      0.611111111
 pop313      pop317      7.74519839      0.321501502
 pop313      pop1031      0.478942882      0.493347826
 pop313      pop252      0.097608176      0.552358596
 pop313      pop254a      3.911428571      0.674545455
 pop313      pop1026      1.608445946      0.594965278
 pop313      popClapier      12.34074074      0.436
 pop313      pop315      0.174410163      0.466081871
 pop313      pop316      0.272222222      0.555113636
 pop313      pop925      2.064209402      0.637613122
 pop313      pop310      0.501960784      0.569791667
 pop313      pop1313      0.045187166      0.710504202
 pop313      pop145      0.025714286      0.601818182
 pop313      pop143      0.061538462      0.620833333
 pop313      popFreaux      0.058155201      0.782298424
 pop195      pop317      2.757964347      0.348168168
 pop195      pop1031      0.259335038      0.505347826
 pop195      pop252      2.212676669      0.569260004
 pop195      pop254a      9.282857143      0.710909091
 pop195      pop1026      5.976013514      0.611631944
 pop195      popClapier      20.4      0.484
 pop195      pop315      2.077858439      0.510526316
 pop195      pop316      2.938888889      0.56875
 pop195      pop925      6.308653846      0.67290724
 pop195      pop310      0.125490196      0.607291667
 pop195      pop1313      1.754278075      0.73907563
 pop195      pop145      0.825714286      0.638181818
 pop195      pop143      0.553846154      0.670833333
 pop195      popFreaux      2.062456277      0.795485238
 pop317      pop1031      15.29997541      0.425215356
 pop317      pop252      28.11497211      0.447806407
 pop317      pop254a      44.74985179      0.448792793
 pop317      pop1026      47.48503144      0.479849167
 pop317      popClapier      66.64076577      0.299376299
 pop317      pop315      16.74822191      0.319477372
 pop317      pop316      34.4482444      0.465370153
 pop317      pop925      35.58215771      0.431908422
 pop317      pop310      7.656476148      0.388150863
 pop317      pop1313      20.22365033      0.500126725
 pop317      pop145      12.9563034      0.408792793
 pop317      pop143      8.885262621      0.383677795
 pop317      popFreaux      29.44327581      0.642853595
 pop1031      pop252      4.241305461      0.514720542
 pop1031      pop254a      18.5129097      0.537345936
 pop1031      pop1026      16.52515329      0.534884952
 pop1031      popClapier      38.14319904      0.471353108
 pop1031      pop315      2.439565424      0.4781559
 pop1031      pop316      7.033822042      0.519910486
 pop1031      pop925      11.86476107      0.527703696
 pop1031      pop310      0.024987506      0.506737859
 pop1031      pop1313      2.338326635      0.559483541
 pop1031      pop145      0.571029357      0.516476371
 pop1031      pop143      0.231884058      0.513535685
 pop1031      popFreaux      4.224632193      0.64002277
 pop252      pop254a      7.186176046      0.600900701
 pop252      pop1026      3.351338895      0.578039683
 pop252      popClapier      22.40441468      0.516890517
 pop252      pop315      0.047109644      0.525020886
 pop252      pop316      0.166106116      0.555088371
 pop252      pop925      3.375603641      0.587313867
 pop252      pop310      2.418171866      0.560597572
 pop252      pop1313      0.019713727      0.623775124
 pop252      pop145      0.495266955      0.572993725
 pop252      pop143      0.543299176      0.575551433
 pop252      popFreaux      0.023520042      0.698657062
 pop254a      pop1026      1.713735955      0.635603448
 pop254a      popClapier      4.407857143      0.584
 pop254a      pop315      3.59215311      0.596290727
 pop254a      pop316      6.08047619      0.595242718
 pop254a      pop925      0.537624434      0.696711146
 pop254a      pop310      12.40870748      0.653049645
 pop254a      pop1313      6.342891326      0.742125903
 pop254a      pop145      8      0.673333333
 pop254a      pop143      6.883902439      0.701538462
 pop254a      popFreaux      8.350843373      0.789142987
 pop1026      popClapier      12.60570988      0.556170886
 pop1026      pop315      1.052988269      0.563230994
 pop1026      pop316      2.334722222      0.576672535
 pop1026      pop925      0.197542735      0.621776661
 pop1026      pop310      8.501893939      0.596172481
 pop1026      pop1313      2.794792917      0.654986213
 pop1026      pop145      4.338455056      0.608017241
 pop1026      pop143      3.6125      0.614583333
 pop1026      popFreaux      4.44001004      0.718804528
 popClapier      pop315      13.7255117      0.40247678
 popClapier      pop316      21.03969072      0.525789474
 popClapier      pop925      7.697898032      0.555581614
 popClapier      pop310      27.64227642      0.495726496
 popClapier      pop1313      19.85294118      0.631452581
 popClapier      pop145      21.5697619      0.524
 popClapier      pop143      18.54545455      0.516129032
 popClapier      popFreaux      24.55834337      0.737644455
 pop315      pop316      0.00215311      0.532311449
 pop315      pop925      1.514721098      0.568908295
 pop315      pop310      1.959200326      0.512623021
 pop315      pop1313      0.091594135      0.639713471
 pop315      pop145      0.537607656      0.53914787
 pop315      pop143      0.601503759      0.535885167
 pop315      popFreaux      0.10506422      0.739733672
 pop316      pop925      2.53766328      0.583931213
 pop316      pop310      3.601282051      0.561601307
 pop316      pop1313      0.231630547      0.61509979
 pop316      pop145      1.051904762      0.571941748
 pop316      pop143      1.008333333      0.57393617
 pop316      popFreaux      0.362070367      0.684715259
 pop925      pop310      8.012820513      0.627337073
 pop925      pop1313      3.092477376      0.719310735
 pop925      pop145      4.506251885      0.647731554
 pop925      pop143      3.989010989      0.669471154
 pop925      popFreaux      4.07878621      0.776336475
 pop310      pop1313      1.656524679      0.683473389
 pop310      pop145      0.522993197      0.601985816
 pop310      pop143      0.266666667      0.614035088
 pop310      popFreaux      2.246668919      0.758309428
 pop1313      pop145      0.268315055      0.70002064
 pop1313      pop143      0.338823529      0.727941176
 pop1313      popFreaux      0.000436134      0.793307244
 pop145      pop143      0.015609756      0.64
 pop145      popFreaux      0.380473003      0.766501478
 pop143      popFreaux      0.438116101      0.786486151

我想制作一个矩阵,每个矩阵分别包含 MSa(矩阵 1)和 MSw(矩阵 2)数据,具有相同的列名和行名的总体名称,例如 MSa 矩阵的以下前四行/列:

             pop314           pop1001          pop311           pop803      ...
 pop314      0                0.142773893      0.722622378      1.208258657
 pop1001     0.142773893      0                2.401578947      0.604514089
 pop311      0.722622378      2.401578947      0                12.78856521
 pop803      1.208258657      0.604514089      12.78856521      0
 ...

其次,我想按这个顺序用人口制作矩阵(我已经按这个顺序制作了一个名为“popnames”的 R 列表):

pop317、pop252、pop254a、pop145、popFreaux、pop803、pop195、pop1001、pop311、pop1026、pop1031、pop310、pop925、pop316、pop315、pop314、pop313、pop130、popClapier、pop1313、popLaVall、pop143

您能提供的任何帮助都会很棒!谢谢!

4

1 回答 1

0

Try:

 MSa.tbl <- xtabs( MSa ~ Pop1 + Pop2 , data=dat)
 MSw.tbl <- xtabs( MSw ~ Pop1 + Pop2 , data=dat)

These will be table objects which inherit from matrices. Generally contingency tables have integer values but that requirement is not enforced. If you wnated to have "true" R matrices, you could wrap as.matrix around them. (Missing values get coded as 0 as would be expected in a contingency table. If you want those entries to be NA then:

is.na(MSa.tbl) <- MSa.tbl==0

There are two formats for such data: "long" as you offered (and is preferred by databases and most of the plotting functions in R) or "wide" as is needed for compact displays and functions like image that expect a matrix layout. There is an as.data.frame.table function that goes "backward" from a table object to the long dataframe representation.

于 2013-09-18T18:21:31.507 回答