【问题标题】:cor() giving a missing value error on small matrix but not large matrixcor() 在小矩阵但不是大矩阵上给出缺失值错误
【发布时间】:2013-12-25 09:42:34
【问题描述】:

我正在尝试使用 cor() 按相关性顺序返回最相关的元素。我编写了这个函数来适应 cor() 来完成它并且它工作得很好,但只有当我在大输入上运行它时。当我尝试在一个小的输入上运行它时,我得到一个 需要 TRUE/FALSE 的缺失值错误,我不明白为什么?

这是我的输入数据示例:

这可以直接复制到R中(通过write.table打印):

"Col2"  "Col3"  "Col4"  "Col5"  "Col6"
"Market Capitalization" NA  NA  17082.69    17879.8 16266.11
"Cash & Equivalents"    NA  NA  747 132 394
"Preferred & Other" NA  NA  0   0   0
"Total Debt"    NA  NA  12379   11982   11309
"Enterprise Value"  NA  NA  28714.69    29729.8 27181.11
"Total Revenue" 2896.75 3461.25 2818    3184    2901
"Growth % YoY"  -0.15   0.68    1.7 3.44    -0.48
"Gross Profit"  NA  NA  1874    2080    1981
"Margin %"  NA  NA  66.5    65.33   68.29
"EBITDA"    758 1074    641 777 699
"Margin %1" 26.17   31.03   22.75   24.4    24.1
"Net Income Before XO"  214.5   410 172 192 207
"Margin %2" 7.4 11.85   6.1 6.03    7.14
"Adjusted EPS"  0.7 1.42    0.59    1.07    0.69
"Growth % YoY1" 0.72    -1.67   -3.28   5.94    -6.76
"Cash from Operations"  375.79  812.21  991 -84 961
"Capital Expenditures"  NA  NA  -660    -676    -608
"Free Cash Flow"    NA  NA  331 -760    353
"Adjusted Price"    2094.66 3689.2  3805.62 3588.42 3582.4

这是我写的 mycor() 函数

mycor<-function(dataset, relative.to=19, neg.cor=0){

#This takes the dataset (as a matrix) and computes the best correleted value
#and returns the row (variable ID) that is the most strongly correlated
#to the variable row referenced by relative.to. Use neg.cor = 1 for neg correlation 

if(neg.cor == 0){
    best.cor <- -1.0 #Have to get better correlation then this
    best.cor.row <- integer() #The row with the best correlation
    all.cor <- numeric() #The correlation for everything else
    index <- 1 #The index for the all.cor array

    for(i in 1:nrow(dataset)){
        if(i != relative.to){ #No self correlation

            temp.cor <- cor(dataset[i,], dataset[relative.to,], use = "na.or.complete")
            all.cor[index] <- temp.cor
            index <- index+1 #I wish the ++ opperator worked in R...
            cat(best.cor)
            pause()
            if(temp.cor > best.cor){ #This remembers the best seen cor value
                best.cor <- temp.cor
                best.cor.row <- i 
            } #End inner if
        } #End outter if
    } #End for loop
}else{
    best.cor <- 1.0 #Have to get better correlation then this
    best.cor.row <- integer() #The row with the best correlation
    all.cor <- numeric() #The correlation for everything else
    index <- 1 #The index for the all.cor array

    for(i in 1:nrow(dataset)){
        if(i != relative.to){ #No self correlation

            temp.cor <- cor(dataset[i,], dataset[relative.to,], use = "na.or.complete")
            all.cor[index] <- temp.cor
            index <- index+1 #I wish the ++ opperator worked in R...

            if(temp.cor < best.cor){ #This remembers the worst seen cor value
                best.cor <- temp.cor
                best.cor.row <- i 
            } #End inner if
        } #End outter if
    } #End for loop
} #End else

return(list(all.cor = all.cor, best.cor.row = best.cor.row))
)

当我尝试运行此程序时,我得到:if (temp.cor > best.cor) { 中的错误:需要 TRUE/FALSE 的缺失值。奇怪的是,mycor 函数可以完美运行,并且当我给它提供相同数据集的更大块时不会出错。

这是同一数据集的较大块。

这也可以复制到R中(通过write.table打印):

"Col2"  "Col3"  "Col4"  "Col5"  "Col6"  "Col7"  "Col8"  "Col9"  "Col10" "Col11" "Col12" "Col13" "Col14" "Col15" "Col16" "Col17" "Col18" "Col19" "Col20" "Col21" "Col22" "Col23" "Col24" "Col25" "Col26" "Col27" "Col28" "Col29" "Col30" "Col31" "Col32" "Col33" "Col34" "Col35" "Col36" "Col37" "Col38" "Col39" "Col40" "Col41" "Col42" "Col43" "Col44" "Col45" "Col46" "Col47" "Col48" "Col49" "Col50" "Col51" "Col52" "Col53" "Col54" "Col55" "Col56" "Col57" "Col58" "Col59" "Col60" "Col61" "Col62" "Col63" "Col64" "Col65" "Col66" "Col67" "Col68" "Col69" "Col70" "Col71" "Col72" "Col73" "Col74" "Col75" "Col76" "Col77" "Col78" "Col79" "Col80" "Col81" "Col82" "Col83" "Col84" "Col85" "Col86" "Col87" "Col88" "Col89" "Col90" "Col91" "Col92" "Col93" "Col94" "Col95" "Col96" "Col97" "Col98" "Col99" "Col100"    "Col101"    "Col102"    "Col103"    "Col104"    "Col105"    "Col106"    "Col107"    "Col108"    "Col109"    "Col110"    "Col111"
"Market Capitalization" NA  NA  17082.69    17879.8 16266.11    17540.1 18214.39    17110.13    18167.87    16700.24    15592.71    14824.06    14455.42    13685.56    12168.31    12550.1 12771.45    11273.2 10284.48    10863.21    10655.99    11750.74    10671.37    10818.32    13288.42    12558.8 12221.79    13213.51    12375.92    11854.12    10942.65    10689.79    11364.1 11887.9 11426.1 10249.34    10609.99    10167.51    9600.1  10001.68    9713.38 9184.3  9730.33 8249.64 9160.61 8586.38 8894.55 8908.81 11887.9 11426.1 10249.34    10609.99    10167.51    9600.1  10001.68    9713.38 9184.3  9730.33 8249.64 9160.61 8586.38 8894.55 8908.81 8566.69 8641.04 8444.84 7867.83 8163.04 7238.2  6279.55 6173.33 7376.47 9048.75 10095.35    10351.52    12311.04    12006.02    10785.58    11009.16    9655.09 7990.1  6918.52 7050.24 6844.2  6520.75 6873.11 7489.61 7459.85 7136.58 6930.38 6401.43 6048.8  5843.01 6224.43 6840.76 7529.23 8452.46 8247.48 8132.72 7632.03 7339.11 6549.2  6165.26 6535.8  5793.52 5621.57 5877.31 5391.98 4792.51 5362.35
"Cash & Equivalents"    NA  NA  747 132 394 69  1381    769 648 398 492 516 338 198 178 87  260 75  311 651 74  68  1757    144 210 192 186 157 94  234 63  177 81  119 818 477 26  70  487 55  49  49  60  62  117.86  83.4    59.2    108.34  119 818 477 26  70  487 55  49  49  60  62  117.86  83.4    59.2    108.34  271.35  432.14  41.63   59.57   94.83   72.81   37.66   73.6    485.05  188.94  291.14  57.5    102.29  153.82  105.01  198.26  183.46  269.87  12.23   94.9    106.88  117.28  57.37   103.23  342.29  429.89  48.49   111.39  245.22  360.74  80.65   205.1   36.76   203.96  143.32  74.33   282.45  349.66  384.84  238.24  317.86  315.65  291.01  185.21  353.33  160.33  160.31
"Preferred & Other" NA  NA  0   0   0   0   0   0   213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 257 256 255 255 254 254 254 255 255 255 254 255 255 252 252 253 254 255 221 222 221 221.47  221.13  221.2   220.79  254 255 255 252 252 253 254 255 221 222 221 221.47  221.13  221.2   220.79  222.09  212.56  249.61  212.56  249.61  212.56  212.56  212.56  249.61  212.56  212.56  212.56  249.61  318.02  318.02  318.02  318.02  322.34  322.42  322.54  322.65  322.74  322.77  322.84  639.92  639.98  640.13  640.24  640.31  640.39  640.47  640.54  640.73  640.89  640.95  641.09  641.25  645.87  634.99  635.05  635.18  637.51  637.73  638.05  638.15  640.53  640.77
"Total Debt"    NA  NA  12379   11982   11309   11111   11873   11073   10675   10676   10678   11144   10683   11526   11020   11027   10599   10773   10366   10699   10094   9751    9480    9363    9282    9213    8653    8943    8815    8968    8487    8162    8205    7687    7868    7498    7219    7245    7336    7432    7094    6968    6682    7000    6841.23 6584.25 6374.14 6264.74 7687    7868    7498    7219    7245    7336    7432    7094    6968    6682    7000    6841.23 6584.25 6374.14 6264.74 6234.03 6249.6  6448.51 6100.6  6011.55 5693.56 5536.13 5276.01 5449.52 4792.08 4881.68 4471.08 4312.4  4410.61 4480.08 4437.33 4758.17 4432.04 4532.28 4466.59 4387.54 4313.86 4316.43 4316.66 4146.02 4175.36 4082.33 4085.09 4089.16 4116.98 3970.11 3972.46 3827.89 3850.12 3927.94 3722.68 3709.36 3804.58 3658.69 3885.52 3667.45 3734.29 3737    3615.16 3492.38 3374.62 3229.81
"Enterprise Value"  NA  NA  28714.69    29729.8 27181.11    28582.1 28706.39    27414.13    28407.87    27191.24    25991.71    25665.06    25013.42    25226.56    23223.31    23703.1 23323.45    22184.2 20552.48    21124.21    20888.99    21646.74    18607.37    20294.32    22616.42    21834.8 20943.79    22253.51    21350.92    20842.12    19621.65    18929.79    19743.1 19709.9 18731.1 17525.34    18054.99    17594.51    16702.1 17632.68    17013.38    16324.3 16574.33    15408.64    16105.45    15308.35    15430.68    15286   19709.9 18731.1 17525.34    18054.99    17594.51    16702.1 17632.68    17013.38    16324.3 16574.33    15408.64    16105.45    15308.35    15430.68    15286   14751.46    14671.06    15101.34    14121.44    14329.37    13071.51    11990.59    11588.31    12590.55    13864.46    14898.46    14977.66    16770.77    16580.82    15478.67    15566.25    14547.82    12474.62    11760.98    11744.46    11447.51    11040.07    11454.93    12025.88    11903.5 11522.02    11604.35    11015.38    10533.05    10239.65    10754.35    11248.66    11961.09    12739.51    12673.05    12422.15    11700.18    11439.9 10458.04    10447.58    10520.58    9849.67 9705.29 9945.31 9169.17 8647.34 9072.61
"Total Revenue" 2896.75 3461.25 2818    3184    2901    3438    2771    3078    2915    3629    2993    3349    3140    3707    3017    3462    3273    3489    2845    3423    2998    3858    3149    3577    3228    3579    2957    3357    2649    3441    2555    3317    3107    3337    2395    2800    2181    2734    2164    2685    2279    2801    2176    2570    2057.03 2539.49 1848    2056    3337    2395    2800    2181    2734    2164    2685    2279    2801    2176    2570    2057.03 2539.49 1848    2056    1942.6  2627.56 2112.22 2886.26 2250.13 2820.78 2041.89 2318.59 1963.38 2346.24 1479.08 1776.59 1617.34 2061.62 1561.04 1853.05 1720.06 2011.03 1504.01 1886.15 1632.3  1920.34 1539.73 1867.36 1528.38 1879.88 1459.85 1668.79 1461.25 1821.99 1392.09 1697.76 1483.61 1799.69 1396.01 1586.08 1478.81 1717.88 1280.11 1456.11 1342.73 1720.3  1330.65 1479.39 1367.21 1613.83 1263.27
"Growth % YoY"  -0.15   0.68    1.7 3.44    -0.48   -5.26   -7.42   -8.09   -7.17   -2.1    -0.8    -3.26   -4.06   6.25    6.05    1.14    9.17    -9.56   -9.65   -4.31   -7.13   7.8 6.49    6.55    21.86   4.01    15.73   1.21    -14.74  3.12    6.68    18.46   42.46   22.06   10.67   4.28    -4.3    -2.39   -0.55   4.47    10.79   10.3    17.75   25  5.89    -3.35   -12.51  -28.77  22.06   10.67   4.28    -4.3    -2.39   -0.55   4.47    10.79   10.3    17.75   25  5.89    -3.35   -12.51  -28.77  -13.67  -6.85   3.44    24.48   14.6    20.23   38.05   30.51   21.4    13.81   -5.25   -4.13   -5.97   2.52    3.79    -1.75   5.38    4.72    -2.32   1.01    6.8 2.15    5.47    11.9    4.59    3.18    4.87    -1.71   -1.51   1.24    -0.28   7.04    0.32    4.76    9.05    8.93    10.13   -0.14   -3.8    -1.57   -1.79   6.6 5.33    -1.02   NA  NA  NA
"Gross Profit"  NA  NA  1874    2080    1981    2393    1934    1993    1846    2244    1794    2000    1942    2103    1723    1826    1700    1979    1558    1551    1459    1531    1420    1588    1478    1595    1317    1506    1273    1554    1202    1322    1179    1460    1097    1217    916 1285    980 1169    1066    1349    975 1157    1024.93 1317.57 980 1091    1460    1097    1217    916 1285    980 1169    1066    1349    975 1157    1024.93 1317.57 980 1091    1052.71 1368.8  1091.61 1236.41 991.8   1374.86 1043.29 1236.87 1129.87 1507.31 998.19  1190.69 1151.22 1475.08 1025.84 1170.8  1115.9  1438.56 981.96  1159.37 1094.25 1401.25 1001.2  1198.64 1079.65 1405.45 984.46  1196.22 1086.13 1415.37 998.06  1177.1  1086.53 1381.01 971.41  1118.91 1055.19 1331.37 947.22  1036.88 991.58  1301.1  921.48  994.97  967.89  1217.32 848.39
"Margin %"  NA  NA  66.5    65.33   68.29   69.6    69.79   64.75   63.33   61.84   59.94   59.72   61.85   56.73   57.11   52.74   51.94   56.72   54.76   45.31   48.67   39.68   45.09   44.39   45.79   44.57   44.54   44.86   48.06   45.16   47.05   39.86   37.95   43.75   45.8    43.46   42  47  45.29   43.54   46.77   48.16   44.81   45.02   49.83   51.88   53.03   53.06   43.75   45.8    43.46   42  47  45.29   43.54   46.77   48.16   44.81   45.02   49.83   51.88   53.03   53.06   54.19   52.09   51.68   42.84   44.08   48.74   51.09   53.35   57.55   64.24   67.49   67.02   71.18   71.55   65.72   63.18   64.88   71.53   65.29   61.47   67.04   72.97   65.02   64.19   70.64   74.76   67.44   71.68   74.33   77.68   71.7    69.33   73.24   76.74   69.58   70.55   71.35   77.5    74  71.21   73.85   75.63   69.25   67.26   70.79   75.43   67.16
"EBITDA"    758 1074    641 777 699 1091    711 794 684 978 617 844 708 916 640 696 625 885 569 611 567 586 520 702 596 715 510 694 547 670 467 564 423 717 411 533 274 624 367 497 458 669 334 485 388.44  693.3   384 487 717 411 533 274 624 367 497 458 669 334 485 388.44  693.3   384 487 445 695.27  439.32  538.75  377.16  666.39  492.65  526.86  446.87  748.34  331.51  492.91  430.87  760.5   313.33  474.78  434.79  751.92  280.96  463.41  390.79  712.97  313.14  490.27  368.26  711.24  307.36  506.85  383.64  721.41  317.3   474.34  363.04  678.27  279.09  400.41  320.03  637.82  281.47  340.21  297.39  610.07  247.48  300.27  305.15  561.67  203.06
"Margin %1" 26.17   31.03   22.75   24.4    24.1    31.73   25.66   25.8    23.46   26.95   20.61   25.2    22.55   24.71   21.21   20.1    19.1    25.37   20  17.85   18.91   15.19   16.51   19.63   18.46   19.98   17.25   20.67   20.65   19.47   18.28   17  13.61   21.49   17.16   19.04   12.56   22.82   16.96   18.51   20.1    23.88   15.35   18.87   18.88   27.3    20.78   23.69   21.49   17.16   19.04   12.56   22.82   16.96   18.51   20.1    23.88   15.35   18.87   18.88   27.3    20.78   23.69   22.91   26.46   20.8    18.67   16.76   23.62   24.13   22.72   22.76   31.9    22.41   27.74   26.64   36.89   20.07   25.62   25.28   37.39   18.68   24.57   23.94   37.13   20.34   26.25   24.09   37.83   21.05   30.37   26.25   39.59   22.79   27.94   24.47   37.69   19.99   25.25   21.64   37.13   21.99   23.36   22.15   35.46   18.6    20.3    22.32   34.8    16.07
"Net Income Before XO"  214.5   410 172 192 207 440 214 280 193 386 168 314 236 353 186 229 205 339 153 183 163 185 283 303 209 313 154 261 205 234 129 183 148 290 121 184 55  253 92  158 50  260 69  157 123.03  286.54  101 169 290 121 184 55  253 92  158 50  260 69  157 123.03  286.54  101 169 128.51  280.74  104.07  182.51  49.48   283.27  72.14   191.53  124.96  339.41  69.8    180.05  135.23  351.55  66.51   176.45  143.61  355.04  47.56   166.61  120.15  327.99  71.42   188.48  113.12  333.3   76.4    201.03  117.88  339.87  87.21   189.31  117.29  324.84  62.45   153.94  100.63  309.44  77.54   116.48  92.2    303.36  64.65   106.7   121.1   263.26  49.06
"Margin %2" 7.4 11.85   6.1 6.03    7.14    12.8    7.72    9.1 6.62    10.64   5.61    9.38    7.52    9.52    6.17    6.61    6.26    9.72    5.38    5.35    5.44    4.8 8.99    8.47    6.47    8.75    5.21    7.77    7.74    6.8 5.05    5.52    4.76    8.69    5.05    6.57    2.52    9.25    4.25    5.88    2.19    9.28    3.17    6.11    5.98    11.28   5.47    8.22    8.69    5.05    6.57    2.52    9.25    4.25    5.88    2.19    9.28    3.17    6.11    5.98    11.28   5.47    8.22    6.62    10.68   4.93    6.32    2.2 10.04   3.53    8.26    6.36    14.47   4.72    10.13   8.36    17.05   4.26    9.52    8.35    17.65   3.16    8.83    7.36    17.08   4.64    10.09   7.4 17.73   5.23    12.05   8.07    18.65   6.26    11.15   7.91    18.05   4.47    9.71    6.8 18.01   6.06    8   6.87    17.63   4.86    7.21    8.86    16.31   3.88
"Adjusted EPS"  0.7 1.42    0.59    1.07    0.69    1.44    0.61    1.01    0.74    1.33    0.57    0.99    0.69    1.32    0.51    0.93    0.67    1.16    0.48    0.78    0.72    0.98    0.42    0.87    0.71    1.2 0.58    1.03    0.78    0.92    0.51    0.86    0.59    1.17    0.48    0.75    0.49    1.08    0.38    0.69    0.65    1.16    0.29    0.72    0.56    1.33    0.46    0.78    1.17    0.48    0.75    0.49    1.08    0.38    0.69    0.65    1.16    0.29    0.72    0.56    1.33    0.46    0.78    0.59    1.3 0.48    0.84    0.52    1.4 0.33    0.88    0.57    1.5 0.3 0.76    0.56    1.49    0.26    0.73    0.59    1.49    0.18    0.69    0.49    1.38    0.28    0.78    0.44    1.38    0.29    0.82    0.47    1.41    0.33    0.77    0.46    1.35    0.23    0.62    0.39    1.3 0.3 0.47    0.36    1.29    0.24    0.43    0.49    1.11    0.18
"Growth % YoY1" 0.72    -1.67   -3.28   5.94    -6.76   8.27    7.02    2.02    7.25    0.76    11.76   6.45    2.99    13.79   6.25    19.23   -6.94   18.37   14.29   -10.34  1.41    -18.33  -27.59  -15.53  -8.97   30.43   13.73   19.77   32.2    -21.37  6.25    14.67   20.41   8.33    26.32   8.7 -24.62  -6.9    31.03   -4.17   16.07   -12.78  -36.96  -7.69   -5.08   2.31    -4.17   -7.14   8.33    26.32   8.7 -24.62  -6.9    31.03   -4.17   16.07   -12.78  -36.96  -7.69   -5.08   2.31    -4.17   -7.14   13.46   -7.14   45.45   -4.55   -8.77   -6.67   10  15.79   1.79    0.67    13.64   4.11    -5.08   -0.07   44.44   5.89    20.41   8.05    -34.72  -11.62  11.36   0   -3.45   -4.88   -6.38   -2.13   -12.12  6.49    2.17    4.44    43.48   24.19   17.95   3.85    -23.33  31.91   8.33    0.78    25  9.3 -26.53  16.22   33.33   -23.21  NA  NA  NA
"Cash from Operations"  375.79  812.21  991 -84 961 391 845 402 976 572 1227    362 1407    179 794 1   997 26  798 645 581 -1237   733 563 630 109 346 481 710 -162    224 593 177 581 -346    389 525 164 490 152 766 218 492 -58 735.49  285 369 146 581 -346    389 525 164 490 152 766 218 492 -58 735.49  285 369 146 490.18  387.73  254.59  141.41  215.82  279.84  489.5   199.17  -325.31 -66.66  280.22  256.65  718.82  438.66  302.05  244.37  -52.38  647.78  53.19   258.9   294.29  359.1   267.8   184.51  310.07  585.52  233.75  145.31  426.63  480.57  187.86  270.34  236.08  472.92  243.13  69.8    261.19  291.41  285.57  77.33   283.64  328.4   309.68  11.95   357.21  141.59  357.15
"Capital Expenditures"  NA  NA  -660    -676    -608    -478    -635    -523    -542    -503    -629    -460    -599    -548    -551    -465    -719    -531    -595    -529    -785    -584    -608    -547    -638    -519    -485    -482    -583    -480    -537    -420    -619    -385    -426    -390    -431    -439    -308    -373    -448    -356    -404    -317    -593.69 -310    -392    -340    -385    -426    -390    -431    -439    -308    -373    -448    -356    -404    -317    -593.69 -310    -392    -340    -302.22 -394.08 -274.8  -228.02 -75.57  -274.36 -684.94 -207.41 -211.95 -218.98 -157.07 -127.56 -210.59 -156.81 -150.58 -127.3  -226.32 -145.55 -171.37 -140.37 -244.12 -167.92 -185.35 -142.94 -239.55 -165.98 -166.25 -147.38 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
"Free Cash Flow"    NA  NA  331 -760    353 -87 210 -121    434 69  598 -98 808 -369    243 -464    278 -505    203 116 -204    -1821   125 16  -8  -410    -139    -1  127 -642    -313    173 -442    196 -772    -1  94  -275    182 -221    318 -138    88  -375    141.79  -25 -23 -194    196 -772    -1  94  -275    182 -221    318 -138    88  -375    141.79  -25 -23 -194    187.96  -6.35   -20.21  -86.61  140.26  5.47    -195.45 -8.24   -537.26 -285.64 123.15  129.09  508.23  281.85  151.46  117.07  -278.7  502.23  -118.18 118.53  50.17   191.18  82.45   41.57   70.51   419.54  67.49   -2.08   426.63  480.57  187.86  270.34  236.08  472.92  243.13  69.8    261.19  291.41  285.57  77.33   283.64  328.4   309.68  11.95   357.21  141.59  357.15
"Adjusted Price"    2094.66 3689.2  3805.62 3588.42 3582.4  3885.75 3523.13 3554.9  3420.27 3141.36 2984.19 2838.81 2760.09 2517.44 2447.56 2403.89 2188.98 1960.8  1952.2  2033.87 2099.97 1993.98 2043.36 2296.42 2201.73 2277.15 2301.5  2203.47 2086.87 1938.95 2019.34 2002.47 2048.12 1881.97 1817.17 1807.02 1664.57 1659.78 1717.25 1585.27 1589.9  1506.13 1534.98 1531.24 1498.21 1528.96 1418.46 1431.1  1343.43 1244.04 1194.62 1076.93 1058.66 960.76  1112.69 1322.69 1414.59 1442.28 1545.6  1364.27 1305.46 1231.15 1022.23 869.37  796.9   820.22  762.84  715.9   756.11  816.37  731.97  705.73  657.84  628.55  571.47  624.67  651.89  676.63  759.77  742.27  734.39  657.44  619.61  569.84  524.2   510.26  475.43  449.8   441.27  409.34  383 413.34  441.72  435.71  419.07  385.87  356.85  346.15  326.97  318.45  323.72  314.18  313.22  300.88  329.3   315.1   312.34  279.11  163.47  NA

较大的块工作得很好,但我需要能够检查较小部分的相关性。我真的是 R 新手,所以这可能很容易,但我已经阅读了这里的板和 r 手册,但找不到它。

【问题讨论】:

  • 在您的第一个数据集之上,缺少引号
  • @vaettchen 已修复。对于那个很抱歉。我一定错过了将数据复制到这个问题

标签: r matrix


【解决方案1】:

在上面的示例中,您的代码在第一个(较小的)数据集上失败,因为第 3 行仅包含 0 和 NA,因此它的标准偏差为 0,因此它与任何其他行的相关性将返回 NA,因为计算相关性包括将样本协方差除以每个向量的样本标准差。在较大的示例中不会发生这种情况,因为第 3 行有足够的变化以具有非零标准差。

但是,您的方法似乎有点复杂。如果要计算矩阵中的单行与所有其他行之间的相关性,按相关性排序,则可以在转置矩阵上使用cor() 并对结果进行排序,例如:

mycor <- function(dataset, relative.to=19) {
  mat <- t(dataset)
  cors <- cor(mat, mat[, relative.to], use="na.or.complete")
  cors[order(drop(cors)), ]
}
mycor(dataset)

【讨论】:

  • 谢谢你,这很有效,而且代码也很有意义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
相关资源
最近更新 更多