四舍五入为整数值的最短方法是使用trunc函数。trunc函数用于返回小于或等于实际值的最大整数,这意味着它将四舍五入为最接近的整数。它用作负数的上限函数和正数的下限函数。
x1<-rnorm(10) x1
输出结果
[1] -0.93535162 -1.14679347 -2.27662044 0.09253004 1.08498688 1.08229470 [7] -1.56573897 0.78489767 -0.38629290 -1.65827949
trunc(x1)
输出结果
[1] 0 -1 -2 0 1 1 -1 0 0 -1
x2<-rnorm(100) x2
输出结果
[1] -0.1414823352 -1.3585058194 0.7731454121 0.7821263500 0.2442938528 [6] -2.0817212231 -0.0008028992 0.6581016302 1.2887699176 0.9071206029 [11] -0.6550945154 0.9511328575 2.2129810414 -0.3977773881 2.3271487866 [16] 0.7835937392 1.6285862580 -1.5902445387 0.3107663715 0.7509937169 [21] 0.7337676334 -2.3734306093 1.0640274101 -0.0108014452 0.2116643255 [26] 0.0627927496 -0.6757863698 2.3442533811 -0.2619344582 0.4920673688 [31] -1.5223007625 -2.4126448370 0.7758588037 -2.3918544772 1.0437406610 [36] 1.0328777428 -1.3034280552 -0.6728730490 1.5060423838 -0.1391259300 [41] 0.2757522407 -0.9150750765 -1.9817158103 -1.4005728911 1.4348993874 [46] 0.9374782742 0.7571684984 -0.6237834981 0.4833934372 1.4562884688 [51] 1.7607699429 1.2149402900 -0.2367143496 -1.2354363417 -0.1620166116 [56] -1.3290830059 1.1440886424 1.0374572556 0.0479191384 0.1691752998 [61] 0.8362809840 0.2195124250 0.8129728720 0.0039731370 0.5173776826 [66] -0.6759989238 0.4878513893 1.6051260796 -0.5055311194 0.9786219788 [71] -1.2027139371 1.6092230819 0.8202643515 0.1083157153 -0.8570305655 [76] 0.3108998172 -1.9558188122 -0.4884092325 -0.3433670990 -1.5875288219 [81] -0.0410887860 1.0684439669 0.0483136250 -0.9337548843 -0.0359380002 [86] 0.0924636861 -0.0208212169 0.0748967138 2.1006328367 -0.4832896587 [91] 0.3659771504 2.1843690548 -0.0993077210 0.2956444441 -0.4813323779 [96] 0.5825952180 -0.3483721799 -0.0645541573 0.6675740121 -0.5026046827
trunc(x2)
输出结果
[1] 0 -1 0 0 0 -2 0 0 1 0 0 0 2 0 2 0 1 -1 0 0 0 -2 1 0 0 [26] 0 0 2 0 0 -1 -2 0 -2 1 1 -1 0 1 0 0 0 -1 -1 1 0 0 0 0 1 [51] 1 1 0 -1 0 -1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 -1 1 0 0 0 [76] 0 -1 0 0 -1 0 1 0 0 0 0 0 0 2 0 0 2 0 0 0 0 0 0 0 0
x3<-runif(100,2,10) x3
输出结果
[1] 7.954663 2.943350 4.712708 5.296338 9.706760 9.866770 9.882452 6.770834 [9] 9.624432 5.128949 7.721584 7.004225 6.933235 7.751586 3.427621 9.928076 [17] 5.730870 2.108947 3.212954 4.671083 9.747722 8.221782 6.156657 2.586116 [25] 2.156852 9.631477 4.177436 3.219972 7.177883 3.345127 7.902857 9.300351 [33] 3.544448 8.192830 9.240767 9.058416 4.155510 9.743593 7.185758 4.181747 [41] 9.438616 9.887865 9.599023 9.829870 9.449224 4.913715 2.826641 2.585879 [49] 4.413621 3.977431 6.558506 8.977440 7.113861 3.107348 9.704511 2.989129 [57] 8.798406 6.072050 5.050253 5.455331 8.163404 3.327951 3.491211 6.566275 [65] 5.262739 6.916546 6.466604 9.728167 3.051050 7.604732 3.022507 6.666468 [73] 4.187495 3.737504 4.302382 5.882281 2.007831 4.809714 9.646016 3.398307 [81] 6.849424 2.351652 5.959438 4.455610 5.674491 7.640682 2.660641 8.766593 [89] 2.648976 4.102925 4.287197 2.355979 4.610613 5.991896 5.728126 3.304057 [97] 8.121376 5.075187 3.828523 6.915818
trunc(x3)
输出结果
[1] 7 2 4 5 9 9 9 6 9 5 7 7 6 7 3 9 5 2 3 4 9 8 6 2 2 9 4 3 7 3 7 9 3 8 9 9 4 [38] 9 7 4 9 9 9 9 9 4 2 2 4 3 6 8 7 3 9 2 8 6 5 5 8 3 3 6 5 6 6 9 3 7 3 6 4 3 [75] 4 5 2 4 9 3 6 2 5 4 5 7 2 8 2 4 4 2 4 5 5 3 8 5 3 6
x4<-rnorm(80,25,3.24) x4
输出结果
[1] 24.42208 21.90653 20.75834 22.02546 24.53996 24.93538 23.08860 24.03999 [9] 23.03142 25.23450 24.90510 20.62063 26.03913 22.27711 22.91115 24.51232 [17] 26.07080 22.58434 26.61411 19.39058 24.16763 24.30714 26.57230 25.46133 [25] 24.81096 21.44637 23.71686 25.24493 24.38496 23.60767 22.68063 29.33581 [33] 23.91165 26.30062 27.30871 26.55297 27.76002 24.20861 29.42817 27.68866 [41] 28.44509 24.77211 26.06472 27.47232 22.15039 19.09000 19.83484 23.98287 [49] 26.75674 26.50842 27.46479 24.63504 26.16109 22.05653 21.48255 20.73107 [57] 23.86625 29.25562 22.42061 25.33801 26.60932 22.55855 20.37860 25.97264 [65] 23.01710 23.69215 28.73903 27.84084 19.09732 30.34330 24.82019 25.36460 [73] 23.02290 26.39190 19.43367 20.38271 26.03162 26.94317 21.56546 28.45962
trunc(x4)
输出结果
[1] 24 21 20 22 24 24 23 24 23 25 24 20 26 22 22 24 26 22 26 19 24 24 26 25 24 [26] 21 23 25 24 23 22 29 23 26 27 26 27 24 29 27 28 24 26 27 22 19 19 23 26 26 [51] 27 24 26 22 21 20 23 29 22 25 26 22 20 25 23 23 28 27 19 30 24 25 23 26 19 [76] 20 26 26 21 28
x5<-rnorm(100,5000,327.25) x5
输出结果
[1] 4730.175 5333.495 5537.905 5589.787 4727.254 5410.457 4893.046 4868.816 [9] 4707.777 5033.363 5007.099 4531.551 4707.251 5007.106 4275.045 5449.996 [17] 4736.522 4885.891 4938.445 4811.098 4166.408 5026.567 4970.116 5048.687 [25] 4925.214 5223.449 5548.780 4519.329 5516.181 5307.102 5096.478 5452.347 [33] 5277.472 5036.306 4365.576 5218.056 5033.874 5444.652 5205.017 4286.010 [41] 5172.966 4964.987 5046.321 4962.531 5218.152 4596.719 4257.091 5605.370 [49] 4706.488 5194.869 4793.121 5056.934 4977.399 4886.530 4880.743 4869.798 [57] 5034.730 4975.458 5255.223 4852.691 5346.542 4993.158 5395.958 5091.647 [65] 4869.546 5364.509 3954.740 5547.372 4639.710 5220.282 4850.229 4468.973 [73] 5138.139 4585.247 4874.217 5179.014 5542.469 5176.163 5473.690 4819.832 [81] 4771.856 4834.502 5355.929 4693.198 5466.436 5079.123 5344.066 4876.401 [89] 4858.778 4935.385 5006.798 5327.960 4792.531 5187.225 4673.725 5205.424 [97] 5379.026 5030.938 4788.946 4926.931
trunc(x5)
输出结果
[1] 4730 5333 5537 5589 4727 5410 4893 4868 4707 5033 5007 4531 4707 5007 4275 [16] 5449 4736 4885 4938 4811 4166 5026 4970 5048 4925 5223 5548 4519 5516 5307 [31] 5096 5452 5277 5036 4365 5218 5033 5444 5205 4286 5172 4964 5046 4962 5218 [46] 4596 4257 5605 4706 5194 4793 5056 4977 4886 4880 4869 5034 4975 5255 4852 [61] 5346 4993 5395 5091 4869 5364 3954 5547 4639 5220 4850 4468 5138 4585 4874 [76] 5179 5542 5176 5473 4819 4771 4834 5355 4693 5466 5079 5344 4876 4858 4935 [91] 5006 5327 4792 5187 4673 5205 5379 5030 4788 4926