在向量的元素上进行迭代与直接在向量上执行操作之间的区别

时间:2019-04-26 19:34:00

标签: r

因此,对于我最近的R类入门课程之一,我们被要求编写一个函数,该函数接受一个向量作为输入,然后通过减去均值并将整个事物除以标准差来更改每个元素。然后,我应该返回这个新向量的标准偏差。 作为一个初学者,我选择创建一个遍历每个元素并执行以下操作的for循环: ``

 for(i in 1:length(vec)){
    vec[i] <- (vec[i] - mean(vec))/sd(vec)
 }

``

但是我得到了错误的输出,最终发现我应该对整个向量本身而不是对每个元素进行运算: ``

 vec <- (vec - mean(vec))/sd(vec)

``

两种方法的结果存在实际差异。第一种方法给了我: ``

 [1] -1.70622042 -1.66806653 -1.62880198 -1.58852738 -1.54734431 -1.50535419
  [7] -1.46265728 -1.41935181 -1.37553311 -1.33129297 -1.28671898 -1.24189407
 [13] -1.19689615 -1.15179781 -1.10666616 -1.06156271 -1.01654339 -0.97165858
 [19] -0.92695320 -0.88246689 -0.83823422 -0.79428484 -0.75064379 -0.70733173
 [25] -0.66436520 -0.62175690 -0.57951591 -0.53764803 -0.49615595 -0.45503952
 [31] -0.41429598 -0.37392017 -0.33390472 -0.29424023 -0.25491543 -0.21591735
 [37] -0.17723142 -0.13884163 -0.10073057 -0.06287959 -0.02526878  0.01212288
 [43]  0.04931753  0.08633842  0.12320983  0.15995716  0.19660684  0.23318646
 [49]  0.26972474  0.30625161  0.34279830  0.37939741  0.41608302  0.45289080
 [55]  0.48985822  0.52702462  0.56443150  0.60212269  0.64014461  0.67854658
 [61]  0.71738116  0.75670451  0.79657685  0.83706297  0.87823279  0.92016207
 [67]  0.96293319  1.00663604  1.05136912  1.09724076  1.14437067  1.19289164
 [73]  1.24295167  1.29471649  1.34837262  1.40413106  1.46223186  1.52294970
 [79]  1.58660093  1.65355232  1.72423224  1.79914503  1.87888958  1.96418394
 [85]  2.05589811  2.15509866  2.26311049  2.38160407  2.51272174  2.65926551
 [91]  2.82498523  3.01503756  3.23674985  3.50096197  3.82454625  4.23556347
 [97]  4.78509150  5.57913167  6.88932519  9.77001045

``

第二种方法给了我: ``

 [1] -1.70622042 -1.67175132 -1.63728222 -1.60281312 -1.56834402 -1.53387492
  [7] -1.49940582 -1.46493672 -1.43046762 -1.39599852 -1.36152943 -1.32706033
 [13] -1.29259123 -1.25812213 -1.22365303 -1.18918393 -1.15471483 -1.12024573
 [19] -1.08577663 -1.05130753 -1.01683843 -0.98236933 -0.94790023 -0.91343113
 [25] -0.87896203 -0.84449293 -0.81002384 -0.77555474 -0.74108564 -0.70661654
 [31] -0.67214744 -0.63767834 -0.60320924 -0.56874014 -0.53427104 -0.49980194
 [37] -0.46533284 -0.43086374 -0.39639464 -0.36192554 -0.32745644 -0.29298734
 [43] -0.25851825 -0.22404915 -0.18958005 -0.15511095 -0.12064185 -0.08617275
 [49] -0.05170365 -0.01723455  0.01723455  0.05170365  0.08617275  0.12064185
 [55]  0.15511095  0.18958005  0.22404915  0.25851825  0.29298734  0.32745644
 [61]  0.36192554  0.39639464  0.43086374  0.46533284  0.49980194  0.53427104
 [67]  0.56874014  0.60320924  0.63767834  0.67214744  0.70661654  0.74108564
 [73]  0.77555474  0.81002384  0.84449293  0.87896203  0.91343113  0.94790023
 [79]  0.98236933  1.01683843  1.05130753  1.08577663  1.12024573  1.15471483
 [85]  1.18918393  1.22365303  1.25812213  1.29259123  1.32706033  1.36152943
 [91]  1.39599852  1.43046762  1.46493672  1.49940582  1.53387492  1.56834402
 [97]  1.60281312  1.63728222  1.67175132  1.70622042

``

有人可以解释为什么会这样吗?我真的很困惑。

0 个答案:

没有答案