矩阵不会填充值 - R.

时间:2014-01-30 02:37:18

标签: r

我试图获取月度股票回报数据并将其乘以年度数据。当我运行for循环时,我没有错误,它们似乎工作但是当我查看时,hpryearly矩阵仍然充满NA,而不是它应该计算的实际数字。

我的代码是:

data=read.csv(file.choose())
#Make data more readable. Rows become dates, columns become PERMNOs.
data4=dcast(data,date~PERMNO,value.var='RET')
datamatrix=data.matrix(data4)
#HPR
datamatrix2=datamatrix+1
#Fill in missing values
datamatrix2[is.na(datamatrix2)]=1
datamatrix2[datamatrix2==""]=1
#Calculate yearly HPR
hpryearly=matrix(nrow=nrow(datamatrix2)/12,ncol=ncol(datamatrix2))
for (i in 1:(nrow(datamatrix2)/12)){
  for (j in (i*12-11):(i*12)){
    hpryearly[i,]=hpryearly[i,]*datamatrix2[j,]
  }    
}

datamatrix2如下所示:

date    10001   10002   10025   10026   10028   10032   10042   10044   10051   10056   10065   10066   10078   10085   10092   10100   10104   10107   10113   10116   10119   10137   10138   10143   10145   10147   10151   10158   10163   10180   10182   10200   10201   10205   10207   10211   10220   10223   10225   10232   10238   10239   10252   10253   10257   10258   10259   10271   10272   10290   10294   10297   10299   10302   10304   10308   10318   10324   10333   10342   10353   10355   10362   10363   10371   10375   10382   10383   10395   10397   10404   10421   10443   10463   10488   10501   10507   10514   10516   10517   10530   10547   10550   10560   10562   10563   10568   10574   10586   10597   10606   10623   10629   10638   10644   10645   10649   10650   10656

1 20060132 0.983505 1.029851 1.040000 1.019357 1.005025 1.244943 1.287671 0.855861 1.187391 1.003293 1.038247 1 1.073986 1.028724 1.094328 1.018335 1.029484 1.076482 1.000000 1.000000 1.003708 1.099210 1.061086 1.004280 1.031409 0.983847 1.000000 1.000000 1.191500 0.921053 1.040899 1.187500 1.000000 0.990202 1.088143 1.038733 1.000000 1.000000 0.960779 0.988636 0.988764 1.032922 1.033649 1.094071 1.375000 1.058511 1.063719 0.984848 1.125899 1.040000 1.144518 1.040706 1.035764 1.188070 0.997369 1.062054 1.124623 1.002261 1.037500 1.050525 0.934708 0.944273 1.011940 1.803249 1.036704 0.929256 1.177281 1.083942 1.024306 1.095939 1.078125 1.086374 0.950486 0.986460 0.893581 1.260589 1.100783 0.919298 1.277372 1.131443 1.244042 0.955556 1.081 633 0.960396 1.011101 1.158196 0.945058 1.342474 1.072270 1.051948 1.111918 1.025669 0.970046 1.369110 1.218543 0.989316 1.076447 1 1.094225 2 20060229 0.989463 1.065700 1.035385 1.121202 0.997500 1.185447 0.872340 1.107681 0.898230 1.002645 1.003070 1 0.926667 1.004380 0.990099 1.068126 0.988067 0.957727 1.000000 1.000000 0.997537 1.027881 1.004579 1.153409 1.071756 1.046269 1.000000 1.000000 1.072330 1.107143 1.007276 0.936842 1.000000 1.027288 1.040501 1.038445 1.000000 1.000000 1.039354 0.954320 0.972727 1.000000 1.027467 0.921965 0.993939 0.989950 0.898533 1.023692 0.856869 0.955370 0.962085 0.992442 0.990594 1.049025 0.992252 0.987179 0.960626 0.977446 1.012048 0.960048 1.150735 0.934907 0.938053 1.251251 1.279225 1.014806 0.934200 1.084175 1.049922 0.901624 1.140580 0.992803 1.066415 0.819608 0.982987 1.088000 1.052444 1.145038 1.010159 1.116173 0.893745 0.968992 0.977358 0.905122 0.955505 0.965010 1.011919 0.808897 0.988960 1.049383 1.065024 0.959299 0.979216 0.854302 0.918478 1.041037 1.098172 1 1.029167 3 20060332 1.170394 1.009973 1.223997 0.991605 1.127820 1.119487 1.060976 1.028509 1.141215 0.977580 1.021505 1 1.230216 1.008177 1.011177 1.129841 1.102254 1.012653 1.000000 1.000000 0.980247 0.946588 1.022271 0.988300 1.044444 0.972183 1.000000 1.000000 1.041569 1.045161 0.888755 0.831461 1.000000 1.025199 1.044486 1.000000 1.000000 1.000000 1.039716 1.056803 1.019276 1.072727 1.066337 0.995298 1.006098 1.274112 0.992517 0.996667 1.031320 1.134258 1.014251 1.050993 0.951709 0.954392 1.057172 1.031712 1.074523 0.969426 1.363095 1.000000 0.883387 1.012500 0.987421 1.530000 1.127937 1.015379 0.999443 1.124224 1.057569 0.945445 1.011436 1.011391 1.009297 0.923445 0.932692 1.045000 1.013091 0.928333 1.060845 1.000571 1.210408 1.560000 0.949807 1.189112 1.051848 1.053152 1.044712​​ 0.737935 1.023256 0.884706 1.015651 1.042160 1.051243 1.026637 0.934911 1.063278 1.172135 1 0.994602 4 20060429 0.905369 0.932731 1.069499 1.019649 1.031111 1.159436 1.172299 0.978474 0.965468 1.348061 1.002256 1 0.974659 1.000000 1.002327 0.950806 1.065741 0.887541 1.000000 1.000000 1.016456 1.052585 1.076461 0.827726 0.993687 0.991196 1.000000 1.000000 0.986822 1.080247 0.994852 0.943243 1.000000 1.002785 1.022431 1.000000 1.000000 1.000000 0.995907 1.024444 1.146132 0.950659 0.989736 0.979528 1.042424 0.709163 0.951337 0.996504 1.032538 1.010441 1.080139 0.983932 1.016249 1.012389 0.937381 0.973001 1.002601 0.909848 0.816594 1.000000 1.000000 1.009259 1.000000 0.682353 1.076389 1.052039 1.096100 0.895028 0.953831 1.046271 1.028894 0.805261 1.117122 1.411917 1.000000 0.979155 0.997082 0.951526 1.079941 0.995918 0.966457 0.805128 0.752033 1.050602 1.015518 0.947379 1.032483 0.965779 0.983636 0.875000 0.941387 1.150714 0.981287 0.872471 1.088608 0.911424 1.095335 1 1.084125 5 20060532 0.989548 1.049371 0.939415 0.944526 1.081897 0.902893 1.176586 0.878964 1.166915 0.989742 0.971493 1 0.926000 1.000000 0.964016 0.860899 0.974640 0.941615 1.000000 1.000000 0.941470 1.023576 0.939541 1.077155 0.974279 0.947446 1.000000 1.000000 0.892966 0.763810 0.988834 0.876791 1.000000 0.935556 0.944737 1.000000 1.000000 1.000000 0.926027 1.012292 0.882500 0.990491 1.000314 0.682476 0.889535 0.938202 1.001441 1.011747 1.010504 0.923000 0.907258 1.032924 0.950704 0.884615 1.023448 0.968111 0.894942 0.972858 0.957219 1.000000 0.808318 1.027523 1.000000 1.000383 0.740645 0.994043 0.877764 1.027778 0.871275 1.015120 1.025641 0.985575 1.150393 0.990826 1.000000 1.010932 0.959866 1.130189 1.146670 1.004672 0.916410 0.891720 1.021622 0.796351 1.005864 1.016618 0.957303 0.928346 0.927577 0.851064 1.004677 0.968405 0.975268 1.018145 0.985465 0.920564 0.992222 1 0.957447 6 20060631 0.923613 1.088561 1.009515 1.024575 0.952191 0.869819 0.841667 0.962222 1.067688 1.020336 0.997674 1 0.896328 1.000000 0.975918 0.940887 1.018987 1.028698 1.000000 1.000000 0.957672 1.016452 0.959545 1.151642 0.978630 0.857031 1.000000 1.000000 0.940925 0.992519 1.055081 0.954248 1.000000 1.042515 0.966565 1.000000 1.000000 1.000000 0.959595 1.048571 0.990368 1.085106 1.025134 1.254417 0.915033 0.964072 0.678417 0.997568 0.855856 1.151860 1.129286 1.005937 0.992296 0.957839 0.972139 1.049805 1.086956 1.026050 0.949721 1.000000 0.970917 0.922262 1.000000 1.157411 0.921022 0.990637 0.987840 0.750751 1.063076 1.043400 0.972619 1.068876 0.987371 0.959259 1.000000 0.998008 0.962108 1.205342 0.993024 0.988163 1.048760 0.950000 0.925926 0.794203 0.956300 1.120964 0.970892 0.929601 0.868368 1.032143 0.978705 1.089957 0.977681 1.059970 1.005900 0.870349 1.078387 1 0.914379 7 20060732 1.157417 1.171283 1.336326 0.909888 0.962343 0.728442 0.881188 1.093918 0.868421 1.001993 1.006216 1 1.048193 1.000000 1.004935 1.052356 1.033126 1.032618 1.000000 1.000000 1.074306 1.107364 1.092568 1.013046 0.960298 0.925251 1.000000 1.000000 0.844859 1.007538 0.970504 1.027397 1.000000 0.962091 0.992225 1.000000 1.000000 1.000000 1.021265 1.009698 0.803776 0.986274 1.044349 0.914554 0.892857 0.944099 0.962884 1.007010 0.833198 1.000315 0.973118 1.019261 0.965960 1.044704 0.977482 0.988372 0.991556 1.120065 1.000000 1.000000 1.013825 0.990706 1.000000 1.061218 0.983607 1.026087 0.621336 0.908000 0.970105 0.888012 0.550796 0.977823 0.772759 0.830116 1.000000 0.969749 1.003622 0.768698 1.065892 0.908715 0.846293 0.992481 0.937143 0.927007 1.020187 1.057966 1.070732 0.902372 0.939481 0.865052 0.868256 0.928603 1.048827 0.991042 0.914956 0.937609 0.985116 1 1.002890 8 20060832 1.123574 0.965806 0.980743 1.052509 1.304348 0.795345 0.898876 0.987333 0.990358 1.010125 1.024710 1 1.147126 1.000000 0.942296 1.004975 1.046092 1.071904 1.000000 1.000000 0.971558 1.016809 1.066570 1.116202 1.006379 1.147783 1.000000 1.000000 0.960043 0.972569 0.938408 1.076667 1.000000 1.044179 1.021548 1.000000 1.000000 1.000000 1.006481 1.121202 1.130961 0.983698 1.007078 0.929158 1.040000 0.967105 1.538546 1.005798 1.292517 1.134258 0.915340 1.023041 1.055951 1.029625 1.055323 1.062588 0.892425 0.968558 1.000000 1.000000 0.936364 0.989967 1.000000 1.142813 0.668590 0.968785 1.112736 1.039648 1.083980 1.029444 0.966667 0.996701 1.001044 0.976744 1.000000 0.927899 1.102391 1.075676 0.937955 0.979727 1.136191 1.098485 0.932927 1.062992 1.009690 1.037063 0.922551 1.134479 0.993827 0.860000 1.067628 1.005643 1.003930 1.042031 0.996795 1.077586 0.875264 1 1.015850 9 20060930 0.945830 1.055367 0.957763 0.984370 0.833333 0.968719 0.900000 0.972760 0.915160 1.021656 1.022693 1 0.991984 1.000000 1.074919 1.000990 1.132822 1.064202 1.000000 1.000000 1.000000 0.962386 1.089196 1.006976 1.056302 1.028326 1.000000 1.000000 1.208099 0.925641 1.047005 1.052632 1.000000 0.979827 0.990412 1.000000 1.000000 1.000000 1.034573 1.030610 1.011328 0.988975 0.956955 1.090608 0.984538 0.891156 1.070150 0.988471 0.974436 0.977494 1.139087 0.975976 0.915025 1.136189 1.007987 1.013152 0.993973 1.018723 1.111765 1.000000 1.070388 1.017899 1.000000 0.930423 1.046021 1.008439 1.070369 0.995763 1.041884 1.012142 1.119540 1.015722 1.125577 1.028548 1.000000 1.070409 1.100245 1.015075 0.920088 0.974930 0.985331 0.868965 0.875817 0.914815 1.032150 0.997328 0.990370 1.069519 0.986957 0.893023 1.024186 0.998592 1.017715 0.972424 0.926045 0.884364 1.006022 1 0.998582 10 20061032 1.007273 0.878810 1.255781 1.074277 0.948000 1.141667 1.111111 0.959469 1.179331 1.000000 1.028846 1 1.096970 1.000000 1.101212 1.031157 1.041150 1.049726 1.000000 1.000000 1.055518 1.071197 0.988715 1.019984 1.029829 1.022538 1.000000 1.000000 1.055400 1.213296 1.077197 0.976471 1.000000 0.958823 1.016667 1.000000 1.000000 1.000000 1.024497 0.962932 0.942750 1.068126 1.029828 0.932118 1.015704 1.122137 1.404682 1.031921 1.138117 1.031152 0.986850 1.024615 1.004820 0.944851 1.033674 1.081408 1.129864 1.000593 1.116402 1.000000 1.027211 1.010861 1.000000 1.217009 1.296975 0.998859 1.262970 1.191489 0.980421 0.980759 1.162218 1.087276 1.068555 1.337994 1.000000 1.097952 0.988100 1.056106 1.016367 1.083810 0.980434 1.111111 0.925373 1.008097 1.112971 0.979781 1.015094 1.045833 1.013216 0.937500 1.171914 0.914944 1.016207 1.062687 1.090278 1.120888 1.010375 1 1.244318 11 20061131 1.051444 0.987760 0.977031 1.145765 1.037975 1.101734 1.112500 1.132028 0.902062 1.000000 1.033789 1 0.998158 1.000000 0.986791 0.959233 1.031402 1.026123 1.000000 1.000000 1.088086 1.030909 0.915874 0.958464 1.025804 1.070204 1.000000 1.000000 1.052166 1.200913 1.062770 0.879518 1.000000 1.003067 1.133462 1.000000 1.000000 1.000000 1.056400 1.014542 0.971617 1.049865 1.025978 1.141304 1.007692 1.005442 1.245238 0.993701 1.091525 0.985588 0.891022 0.961864 1.032776 1.035735 1.022836 0.926755 1.160555 0.954821 1.075829 1.000000 0.962472 1.046303 1.000000 1.342651 0.932862 1.003458 1.068046 0.875000 0.983004 1.015804 0.989399 0.945986 1.139336 1.043253 1.000000 1.053528 1.182537 1.059375 0.914286 1.069420 1.058785 1.057143 1.072581 0.947791 1.046940 1.060567 1.011152 0.732271 0.944875 1.083333 1.120903 1.034155 1.072357 0.955524 1.066879 1.029604 0.943207 1 0.971461 12 20061230 0.962706 1.059480 1.035749 1.083725 1.032520 0.988820 0.943820 1.003481 1.075714 1.000000 1.017608 1 1.000000 1.000000 1.016174 1.036000 0.899738 1.017030 1.000000 1.000000 1.032768 1.034941 1.014078 0.953393 1.052583 1.006865 1.000000 1.000000 1.033613 1.188213 0.905548 0.962329 1.000000 1.022209 1.010720 1.000000 1.000000 1.000000 1.055501 1.023667 0.953804 0.956196 1.053260 1.057143 0.977099 0.906631 0.973231 0.992756 0.921118 0.984533 1.062947 1.006923 0.943373 0.970098 1.026068 1.004420 0.994798 0.955321 0.969163 1.000000 1.068807 1.038729 1.000000 1.071788 0.937121 1.049770 1.030534 0.951020 1.041279 0.937768 1.241071 1.012688 1.014893 0.908789 1.000000 1.075559 1.026098 0.970501 0.910541 1.084481 1.009881 0.966216 1.090226 1.084746 1.016413 0.974484 1.033333 1.106638 0.947215 1.076923 0.987509 0.974078 1.019531 0.955468 0.925373 0.968390 0.922033 1 1.024089 13 20070132 1.023279 0.841693 0.874695 0.997101 1.078701 0.703518 0.976190 0.914966 1.188579 1.000000 1.002884 1 1.225092 1.000000 1.074094 0.926641 1.001167 1.033490 1.000000 1.000000 1.203399 1.013287 1.096185 0.973128 1.009947 1.059848 1.000000 1.000000 0.913008 0.980800 0.980724 1.120996 1.000000 1.004828 1.011245 1.000000 1.000000 1.000000 0.980443 0.959672 0.975783 1.022709 0.892590 1.067568 0.968750 1.022388 0.951277 1.001824 1.038436 0.976450 1.017497 1.001875 1.026715 1.093657 1.033670 0.969417 0.896612 1.067392 1.168182 1.000000 0.772532 0.970954 1.000000 1.020261 0.934519 0.934446 1.025926 1.111588 0.991905 1.090103 0.936691 1.063896 0.910775 1.166058 1.000000 1.074313 0.978598 0.851064 1.001252 1.004170 1.215247 1.027972 0.930345 1.269531 0.974006 1.039277 1.028708 0.890855 1.053073 0.969048 1.069569 1.374616 0.962780 0.999482 1.596774 1.079478 0.998183 1 1.091435 14 20070229 1.261621 1.033054

1 个答案:

答案 0 :(得分:2)

问题是你没有给矩阵赋予初始值,所以它首先填充NA。然后,将这些NA乘以数字仍然会给NA。您需要使用1初始化它:

hpryearly <- matrix(1, nrow = nrow(datamatrix2)/12, ncol = ncol(datamatrix2))