首先,我有185个50x120 2D矩阵的文件。我将它放入一个大型阵列中(50x120x185)。此数组表示固定纬度(行)和经过185个月的经度(列)的测量值。我按如下方式制作了这个数组:
a <- c(a199709,a199710..)
a <- array( a, dim = c( 50, 120, 185 )
然后我有另一个文件,其中185个观察了一个变量(随着时间的推移),我需要将这个文件与每个经度的每个纬度相关联(例如,第1列的第1行中的观察需要与之相关联)第二个文件的整个列表,通过阵列的185个月)并返回每个纬度经度组合的相关系数和p值(存储在另一个数据帧中)。 以下是数据外观的一小部分示例。大量的NaN然后是价值观。
0.77982613 0.43060419 NaN NaN NaN NaN 1.33500640 2.32644890 2.03347850 2.00072800 1.40690660 1.01990860 NaN NaN NaN NaN NaN NaN NaN NaN NaN 1.39287550 1.61158960 2.27274490 1.91032030 2.45027570 1.35366430 NaN NaN NaN NaN NaN NaN NaN 1.2788333 1.1108858 0.7634852 0.5747378 0.5821554 0.7710934 NaN NaN NaN NaN NaN NaN 1.3938673 1.5590591 1.4409164 1.2314447 0.7270649 NaN 1.7521698 2.0494428 2.0483702 1.3953564 1.9457542 2.3045875 1.7193034 1.9769972 1.7394963 2.6089941 3.2412812 3.0550246 2.4335631 2.7291053 1.7492553 2.6199513 4.0112099 3.4542034 3.1224831 3.2304652 3.3102016 NaN NaN NaN
第二个文件只是对一个变量的185次观察。
0.122
-0.595
-1.9
-0.836
0.413
0.39
0.736
1.93
我很困惑我应该编写什么代码来循环通过并通过时间(从大数组)关联每个纬度和经度,然后返回相关系数和p值的2d数组。 我试过了
for (i in 1:length(a) ) {
但这是阵列的整体尺寸,而不是185个月。任何帮助将不胜感激
答案 0 :(得分:4)
这可以通过apply()
在行和列上完成,意味着超过边距1:2。
set.seed(1);
R <- 50; C <- 120; Z <- 185;
a <- array(rnorm(R*C*Z,3,1),c(R,C,Z));
othervar <- rnorm(Z,3,1);
cormatrix <- apply(a,1:2,cor,othervar);
dim(cormatrix);
## [1] 50 120
head(cormatrix);
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50] [,51] [,52] [,53] [,54] [,55] [,56] [,57] [,58] [,59] [,60] [,61] [,62] [,63] [,64] [,65] [,66] [,67] [,68] [,69] [,70] [,71] [,72] [,73] [,74] [,75] [,76] [,77] [,78] [,79] [,80] [,81] [,82] [,83] [,84] [,85] [,86] [,87] [,88] [,89] [,90] [,91] [,92] [,93] [,94] [,95] [,96] [,97] [,98] [,99] [,100] [,101] [,102] [,103] [,104] [,105] [,106] [,107] [,108] [,109] [,110] [,111] [,112] [,113] [,114] [,115] [,116] [,117] [,118] [,119] [,120]
## [1,] 0.009381754 -0.0006288594 -0.02453642 0.01673731 -0.08580716 0.007424671 0.06199350 0.05912027 0.14082117 0.03879647 0.055429523 0.019285143 0.04440028 -0.006815387 0.08206478 -0.042006557 -0.024248422 -0.02902434 0.159659672 -0.04683462 0.060717379 0.003758565 0.00798371 -0.0001347086 -0.03763708 0.123863894 0.04959017 0.012322157 0.05836862 -0.049110042 -0.05444975 0.08123221 0.0010390183 0.09420507 -0.03078082 -0.01005937 -0.01180137 -0.045123536 0.05101759 -0.1233930542 -0.008169935 -0.07497612 0.09433525 -0.006173357 0.09376699 0.016314593 0.165064512 0.11378452 0.02762109 -0.02527918 -0.22450064 -0.004495199 0.023877207 -0.06183121 -0.002866376 0.0772095130 -0.06255480 -0.09410189 0.052977233 0.046965820 -0.03446252 -0.13692147 0.06580713 -0.042757755 -0.0937777311 -0.021026699 0.12714381 -0.135192573 -0.05007639 0.0003690562 0.0514145914 -0.02731683 -0.032776316 -0.01940886 0.141988410 -0.12669081 0.019639301 0.028097019 0.12775140 0.045924380 -0.02178064 -0.003417877 -0.059169776 -0.083195329 0.004429876 -0.064323599 -0.1110889162 0.12716897 -0.08683802 -0.002588614 -0.08380587 0.0138280811 3.839420e-02 -0.08592716 0.113669987 0.132043915 -0.05482530 -0.02986897 0.01680328 0.01848973 -0.046684532 -0.02844949 0.01322101 0.07270310 0.01985113 0.070332797 -0.056422697 0.037771503 -0.006590428 0.06368096 0.10666286 0.07181643 0.04711943 0.04126084 0.039975691 -0.18649396 -0.007435666 0.09134351 -0.01207895 0.056075186
## [2,] -0.116331573 0.0923793446 0.07025621 0.11268953 -0.09341328 -0.029630250 -0.10947352 -0.04418272 -0.02807828 0.02386072 0.002132798 -0.110044112 0.01225501 0.006851830 -0.02492116 0.003820713 -0.008091216 -0.02768341 0.020127294 -0.03760861 -0.010413744 0.102326634 0.05705752 0.1338741184 0.01190489 0.064499415 0.01552671 0.005409726 0.01620343 -0.007525286 -0.04295918 -0.03339792 -0.0987456011 -0.01204685 0.05313332 -0.03670885 0.08404514 0.048243569 -0.05602869 0.0041083920 -0.032419558 0.03119497 -0.13121234 0.003364733 -0.01951595 -0.008756877 -0.080924051 -0.07556506 -0.02771641 0.03470183 0.03635250 0.040149390 0.055073590 0.01781694 0.046836610 0.0003702334 -0.11301121 0.14319014 -0.014519734 0.116535288 0.11733430 0.09013510 -0.05403307 0.083202752 0.0009623331 -0.109632519 0.02209232 -0.037161549 -0.09357348 0.0260104408 0.0090272967 -0.06379020 0.050145266 -0.05237282 0.017942495 0.03814418 -0.056380938 0.039966267 0.06690379 0.139621605 -0.07479738 -0.001722190 -0.036746808 0.027948845 0.015002796 -0.030443242 -0.0251480704 0.12064589 0.12480864 0.031388366 0.01932396 -0.1783950552 2.445045e-05 -0.03763376 0.035515719 -0.072368405 0.03268960 0.15731762 -0.04897443 -0.13659531 0.009366105 -0.04801242 0.04758195 -0.09133644 -0.15341346 -0.128054439 0.080262476 0.028192586 0.011979831 0.06192066 0.03126506 0.07221314 0.13785280 0.09589378 -0.017280999 -0.06492398 0.059438276 0.08610208 -0.01885071 -0.075813259
## [3,] -0.102779763 -0.0810640571 -0.14257037 0.21088393 0.02821457 0.085329936 0.08303306 -0.08939576 -0.02961662 0.11739539 0.033070762 -0.062070694 0.02723318 -0.027094699 0.10563542 -0.101945980 -0.009950677 0.04301568 0.005573301 -0.02248607 0.187906312 -0.064618262 0.11749309 0.0526250606 0.01181951 0.063567600 -0.08654828 -0.007813194 0.09730824 -0.010440006 0.01482364 0.10870742 0.0573522844 -0.10159761 -0.04079926 0.05757234 0.01011661 -0.006357944 -0.20295265 -0.0271283944 0.105756854 0.06759921 0.22116864 -0.037853189 -0.09691782 0.014576453 -0.003860806 -0.21512090 0.07048106 0.15220401 -0.02923885 0.093530898 -0.023258568 0.08264406 -0.111023329 -0.0256882537 -0.08900776 -0.14289892 0.077069740 0.007620468 -0.03785364 -0.09233717 -0.03708433 -0.071260896 0.0724121854 0.018853465 -0.01186706 -0.024372429 0.04993413 0.1119935009 -0.0364397975 -0.13710092 0.042246517 0.05872645 0.034436143 -0.03331243 -0.030615385 -0.001502851 -0.04515299 0.068611358 -0.03658459 0.070441802 0.054547108 -0.031624484 0.074951736 0.025191173 -0.0002129645 0.05184713 0.02844743 0.004697035 0.05577633 0.0007769113 -1.289537e-01 0.03873076 0.141705409 0.078895503 0.04495972 0.04231232 0.03509554 0.08910173 0.094258416 0.09223471 -0.01638219 0.07631080 0.07978185 -0.012828509 0.097169758 0.034969541 0.091304123 0.09350470 0.04893160 0.02954687 0.01797942 -0.05127672 0.022520253 0.03172993 0.068829318 -0.03097673 -0.12161914 -0.035660140
## [4,] 0.034063334 0.0097314666 0.01583056 -0.03374442 -0.04112704 0.059171305 0.09873172 0.03687629 -0.03268493 -0.13289716 -0.093629015 -0.041942331 0.04406162 -0.015892287 -0.09759957 0.013638675 0.050354306 0.08677888 -0.006438696 0.09563238 -0.019030147 -0.002390758 0.02089608 0.0282919993 0.07698305 -0.011491945 0.15267854 0.018407784 -0.07091455 -0.042939951 0.07557867 -0.05870813 0.0476818422 0.04835405 -0.05121245 -0.04302029 0.02072076 -0.077949484 -0.03696954 -0.0300763473 -0.075886704 0.25191587 -0.01497429 -0.100527767 -0.04890599 0.082095702 0.092025374 -0.01174520 0.05759299 -0.01065051 0.04471870 0.063831081 -0.004683291 -0.02114892 0.043876961 0.0061499728 0.03104001 0.02166661 -0.067751343 0.001920508 -0.07266827 0.03594379 -0.03354531 0.079411339 -0.1053571459 -0.090763333 0.17255371 0.145697279 0.02285522 0.1732643622 0.0233414511 0.01085483 0.193189843 -0.02279045 -0.106866864 0.07343425 0.006559500 -0.062983434 0.01360569 0.088738879 0.05449209 -0.050243711 -0.007191386 0.005578391 -0.065792727 -0.041335570 -0.0321043839 0.15366613 -0.03376157 -0.080949013 -0.10520406 -0.1540438495 1.202180e-01 -0.01378275 -0.026029433 0.007982372 -0.03608147 0.04599500 -0.03915497 0.03649365 0.052831627 0.14436730 0.06247427 0.15922006 -0.05527590 -0.007527601 -0.006620582 -0.013379888 -0.036986466 0.07118657 0.11029837 -0.04455502 -0.01448403 -0.06101502 -0.064451024 0.09564926 -0.009843696 -0.01630749 0.05907338 -0.007810817
## [5,] -0.044288139 -0.0110997734 -0.06559680 -0.08914721 -0.04631635 0.132700267 0.05714202 -0.07519090 -0.04480446 -0.03195851 0.082148695 0.131522150 -0.08575512 0.071334705 0.05621888 -0.040027759 0.045021192 -0.01297316 0.136030190 -0.04812070 -0.037371068 -0.057394117 0.07127473 -0.1104563646 -0.02178471 0.019443556 0.19149548 -0.024915375 -0.03628089 0.092284708 0.11933379 0.03566934 -0.0032188644 0.03256934 0.01175716 -0.03565773 -0.15554001 -0.023409479 -0.01648936 0.0006733765 -0.048372282 0.03360210 -0.05489893 -0.041976034 0.05810873 -0.025535840 -0.029097435 0.03767795 0.12470464 -0.04640887 -0.05615726 -0.123704401 0.064608047 0.07475996 -0.028435546 -0.0915745306 -0.01862307 -0.05528242 -0.081983776 0.126539999 -0.14603674 -0.03970420 0.03149141 0.009208772 -0.1267964371 0.093578063 0.05803233 -0.113644923 0.08979449 0.0524548642 0.0009302473 -0.01141730 0.076919927 -0.03641760 -0.002630968 0.11105552 0.008730732 0.036862054 -0.04384800 -0.020007532 0.06299397 0.006257206 0.049695572 0.068834075 0.079746655 -0.055885967 0.0213984546 0.03200625 0.06552619 -0.004536179 -0.01954207 -0.0632637692 -1.188454e-01 -0.09435444 0.005473028 0.174591160 0.15422706 0.05863548 -0.03145459 0.01162981 0.047044664 -0.02025591 -0.08145507 -0.07707113 -0.06886323 0.066444471 -0.008929386 -0.009307404 0.009424272 0.07567128 0.01266827 -0.07862022 0.06595594 -0.09976305 -0.020778930 0.06715604 0.217838581 0.06241611 -0.12645563 -0.080255300
## [6,] -0.024479219 0.0178104217 -0.02106269 0.11002586 -0.04927055 -0.090894778 -0.06596670 -0.11569483 -0.03700625 -0.13284595 -0.087097988 -0.006407736 0.01685949 0.134845289 -0.01042487 -0.098278399 -0.077533017 -0.03208775 0.036267238 0.02087152 0.008975871 -0.008328983 0.05764703 -0.1511975220 0.01133040 -0.006561614 0.01614919 0.055462546 -0.09065110 -0.002320140 -0.04828880 0.10405238 -0.0007716107 -0.03159428 -0.07151485 0.02780471 -0.04153880 -0.080730838 0.11666703 -0.1579183058 -0.129183340 0.03349451 -0.04357635 0.028409391 0.13877250 -0.038297925 -0.015317646 0.04537157 -0.10906119 0.11970577 -0.08789867 -0.082087011 0.059723946 -0.12802237 0.102845923 -0.0020856305 -0.13199336 0.07667642 -0.004878673 -0.058342448 0.07888365 0.03926886 -0.05096548 0.026496215 -0.1517316141 0.004113867 0.04985069 0.004478391 0.01196789 -0.0721750762 -0.0059740202 0.05486006 -0.005125651 -0.02943793 -0.059784830 0.00926545 0.017492250 0.110121551 -0.01140056 -0.008192348 -0.04803663 0.001858178 -0.078978844 0.128729019 -0.090863206 0.004975692 -0.0314191279 0.10578017 -0.06794299 -0.115434853 -0.11476294 -0.2008461259 -1.483347e-01 0.10069680 -0.026093548 0.017089295 0.01008518 -0.03079210 -0.03278922 -0.06956633 0.014104116 -0.11596993 0.10274567 0.05578534 0.11225170 -0.051697100 0.066064668 0.004772438 -0.071401849 0.02747507 0.02938896 -0.03833709 -0.06820718 -0.04680215 0.007572711 -0.04361055 0.179682576 0.06242033 0.08432312 -0.004680091
如果您运行cor.test()
而不是cor()
,您将获得一个归类为htest
的列表矩阵,相关系数可在$estimate
下访问,p值可在$p.value
下访问:
cormatrix <- apply(a,1:2,cor.test,othervar);
dim(cormatrix);
## [1] 50 120
head(cormatrix);
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50] [,51] [,52] [,53] [,54] [,55] [,56] [,57] [,58] [,59] [,60] [,61] [,62] [,63] [,64] [,65] [,66] [,67] [,68] [,69] [,70] [,71] [,72] [,73] [,74] [,75] [,76] [,77] [,78] [,79] [,80] [,81] [,82] [,83] [,84] [,85] [,86] [,87] [,88] [,89] [,90] [,91] [,92] [,93] [,94] [,95] [,96] [,97] [,98] [,99] [,100] [,101] [,102] [,103] [,104] [,105] [,106] [,107] [,108] [,109] [,110] [,111] [,112] [,113] [,114] [,115] [,116] [,117] [,118] [,119] [,120]
## [1,] List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9
## [2,] List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9
## [3,] List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9
## [4,] List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9
## [5,] List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9
## [6,] List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9 List,9
cormatrix[[1,1]];
##
## Pearson's product-moment correlation
##
## data: newX[, i] and othervar
## t = 0.1269, df = 183, p-value = 0.8991
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.1350697 0.1534427
## sample estimates:
## cor
## 0.009381754
##
cormatrix[[1,1]]$estimate;
## cor
## 0.009381754
cormatrix[[1,1]]$p.value;
## [1] 0.8991434