如何用 R 编程将数字四舍五入到最近的百位数?
要在 R 中将一个值四舍五入,我们可以使用 round 函数;如果要四舍五入到最近的百位数,那么应该使用 -2 值进行四舍五入。举例来说,假设我们有一个向量称之为 x,其中包含 25、78、32、38、79、91、82、20、56,那么 round(x,-2) 的输出如下
0, 100, 0, 0, 100, 100, 100, 0, 100
示例 1
> x1<-rnorm(10,120,50) > x1
输出
[1] 89.61275 141.54670 130.25924 142.81744 38.60795 47.40821 69.30543 [8] 12.15515 174.11419 20.69817
示例
> round(x1,-2)
输出
[1] 100 100 100 100 0 0 100 0 200 0
示例 2
> x2<-rnorm(100,120,60) > x2
输出
[1] 200.095061 206.720294 138.471412 128.208184 151.900210 63.717254 [7] 156.931319 209.009279 176.416783 105.085543 93.546862 128.853476 [13] 148.053905 174.839196 203.737574 146.825702 105.457096 173.874628 [19] 169.803994 60.572176 178.237244 203.810326 80.719865 94.174711 [25] 162.453897 106.240436 127.433929 208.214542 131.028068 92.870231 [31] 140.720894 42.651514 165.289149 13.375160 94.329940 71.876679 [37] 107.241246 96.262988 219.807754 145.722219 108.398741 205.967403 [43] 145.948586 85.032097 227.794284 204.046132 74.620053 115.689188 [49] 169.348128 59.402132 212.328543 161.365704 140.384076 218.065167 [55] 100.907675 161.661667 34.387952 148.238412 151.183663 104.967732 [61] 136.176786 80.512360 95.035262 153.149381 4.655616 167.156364 [67] 14.473762 109.639397 107.070616 47.568150 180.272436 124.611189 [73] 158.350524 171.339213 121.324641 60.166787 65.728890 140.234364 [79] 137.634627 54.564704 101.383017 118.009068 147.360182 99.928836 [85] 5.523774 36.624999 10.115692 237.099438 122.702824 71.494478 [91] 113.543730 162.988237 74.957041 155.995607 96.629868 196.688934 [97] 166.616179 149.185278 95.640113 236.727589
示例
> round(x2,-2)
输出
[1] 200 200 100 100 200 100 200 200 200 100 100 100 100 200 200 100 100 200 [19] 200 100 200 200 100 100 200 100 100 200 100 100 100 0 200 0 100 100 [37] 100 100 200 100 100 200 100 100 200 200 100 100 200 100 200 200 100 200 [55] 100 200 0 100 200 100 100 100 100 200 0 200 0 100 100 0 200 100 [73] 200 200 100 100 100 100 100 100 100 100 100 100 0 0 0 200 100 100 [91] 100 200 100 200 100 200 200 100 100 200
示例 3
> x3<-runif(80,50,200) > x3
输出
[1] 190.27252 155.32879 175.01110 100.87456 152.60590 74.71596 121.35612 [8] 55.87325 121.24243 81.55583 108.66598 100.59884 165.46113 129.17487 [15] 84.19696 112.37490 162.77709 100.26368 104.26728 57.81007 122.87770 [22] 175.91888 60.23992 98.50004 130.80482 170.23349 193.14007 173.45364 [29] 174.91854 131.74613 85.70544 101.73342 62.61430 105.24194 69.58468 [36] 113.18957 196.87726 188.19204 61.03517 76.17451 145.93752 153.57438 [43] 93.97600 106.59288 114.67775 196.36251 55.22318 160.60880 179.77599 [50] 55.62690 95.40597 119.23054 176.63038 94.59722 138.94008 101.09087 [57] 189.16560 65.93981 138.85905 174.91417 173.81721 167.15242 84.75690 [64] 197.05821 164.97665 52.40786 161.50446 92.95925 91.16180 131.55187 [71] 97.13889 96.88271 129.55102 177.42351 160.40981 191.51033 156.36852 [78] 55.18035 160.83181 175.20245
示例
> round(x3,-2)
输出
[1] 200 200 200 100 200 100 100 100 100 100 100 100 200 100 100 100 200 100 100 [20] 100 100 200 100 100 100 200 200 200 200 100 100 100 100 100 100 100 200 200 [39] 100 100 100 200 100 100 100 200 100 200 200 100 100 100 200 100 100 100 200 [58] 100 100 200 200 200 100 200 200 100 200 100 100 100 100 100 100 200 200 200 [77] 200 100 200 200
示例 4
> x4<-rnorm(100,500,120) > x4
输出
[1] 585.7340 472.3567 469.3459 375.4255 444.9098 478.0684 634.6411 379.8119 [9] 602.1881 563.3953 673.9045 486.2639 367.9377 315.7793 346.8235 593.0923 [17] 383.9356 613.3875 566.1341 582.9207 239.6635 444.1799 690.2431 330.5347 [25] 431.7668 598.7132 652.1041 684.2897 454.4928 506.6183 653.4909 450.6463 [33] 579.3951 663.9724 476.6189 451.4790 586.9429 543.9879 518.5954 504.7755 [41] 414.4607 430.8862 537.9323 356.7645 384.5718 533.1949 452.6198 446.6593 [49] 469.5048 500.3851 593.5534 244.6215 395.8466 545.7889 685.6367 268.5527 [57] 414.8149 500.2674 483.4512 444.0095 460.9468 382.2085 511.1722 477.4728 [65] 543.5912 739.8601 744.3788 443.7733 424.7943 500.5329 497.0514 601.4509 [73] 685.6504 594.2415 529.4130 742.5796 572.8989 602.9205 625.1378 543.2260 [81] 403.1793 373.7638 659.8974 468.5121 358.3145 582.3178 374.0248 639.3185 [89] 447.4567 270.1555 610.0241 356.8236 464.9692 615.0988 775.8457 531.8723 [97] 261.7674 617.1434 553.2038 454.2908
示例
> round(x4,-2)
输出
[1] 600 500 500 400 400 500 600 400 600 600 700 500 400 300 300 600 400 600 [19] 600 600 200 400 700 300 400 600 700 700 500 500 700 500 600 700 500 500 [37] 600 500 500 500 400 400 500 400 400 500 500 400 500 500 600 200 400 500 [55] 700 300 400 500 500 400 500 400 500 500 500 700 700 400 400 500 500 600 [73] 700 600 500 700 600 600 600 500 400 400 700 500 400 600 400 600 400 300 [91] 600 400 500 600 800 500 300 600 600 500
广告