How to perform rounding in R to next 10 instead of nearest 10?

R ProgrammingServer Side ProgrammingProgramming

In general, most commonly used rounding is rounding to nearest 10 or nearest 100 but sometimes we actually want to remove the values after a value instead of rounding. For example, removing values after 2 decimal places, this is the type of situation where we need to round to next 10 instead of nearest 10. This can be done with the help of floor function as shown in the below examples.

Example1

 Live Demo

x1<−1.2321412
floor(x1*10)/10

Output

[1] 1.2

Example2

 Live Demo

x2<−rnorm(100)
x2

Output

[1] −1.01607202 −0.13627843 −0.05332404 −0.20988562 0.53192788 −0.21490491
[7] 0.93909670 −0.71803829 0.36707998 0.71611459 0.53648436 0.73775330
[13] −0.04830651 −0.66710417 0.66421934 0.03941354 0.10747146 −0.12769608
[19] 0.84294195 −0.35810057 −1.21310936 −0.65778384 −0.48420552 −0.65712008
[25] 1.46669154 0.80618156 −1.45079725 −0.99269474 0.79892560 1.72865301
[31] 0.59923441 −0.55706118 −0.17318689 −0.03198674 −0.16822942 −0.86734324
[37] 0.93642051 0.46739939 −0.58546721 −0.28062626 −0.08280940 −0.02796720
[43] −1.20691923 0.61127461 −0.56228455 1.58841226 2.60447614 −0.10876521
[49] 1.65494022 0.67555885 0.96882488 −1.23490973 0.53605869 0.78852196
[55] 0.54627918 −0.21922728 −1.35104345 0.85517266 −1.63592878 1.04832402
[61] −0.97162092 0.87955671 0.28617432 −0.49176891 −3.02859514 −0.67549573
[67] −0.50120112 2.03164978 0.35270213 −0.20283306 −0.41561950 −0.73609624
[73] 1.17009724 1.03897592 1.31227239 0.49276017 −0.03460787 −0.94871491
[79] −2.06266496 0.84675274 0.20100772 0.06472715 0.17989075 −0.88503103
[85] 0.50389279 −1.26776074 −0.29691182 0.03318311 1.66971094 0.60210033
[91] 1.45134752 −0.49960181 0.38621056 1.23958281 0.16353699 −1.39379050
[97] 0.53588952 −0.05194538 1.74137139 0.65337123

Example

floor(x2*10)/10

Output

[1] −1.1 −0.2 −0.1 −0.3 0.5 −0.3 0.9 −0.8 0.3 0.7 0.5 0.7 −0.1 −0.7 0.6
[16] 0.0 0.1 −0.2 0.8 −0.4 −1.3 −0.7 −0.5 −0.7 1.4 0.8 −1.5 −1.0 0.7 1.7
[31] 0.5 −0.6 −0.2 −0.1 −0.2 −0.9 0.9 0.4 −0.6 −0.3 −0.1 −0.1 −1.3 0.6 −0.6
[46] 1.5 2.6 −0.2 1.6 0.6 0.9 −1.3 0.5 0.7 0.5 −0.3 −1.4 0.8 −1.7 1.0
[61] −1.0 0.8 0.2 −0.5 −3.1 −0.7 −0.6 2.0 0.3 −0.3 −0.5 −0.8 1.1 1.0 1.3
[76] 0.4 −0.1 −1.0 −2.1 0.8 0.2 0.0 0.1 −0.9 0.5 −1.3 −0.3 0.0 1.6 0.6
[91] 1.4 −0.5 0.3 1.2 0.1 −1.4 0.5 −0.1 1.7 0.6

Example3

 Live Demo

x3<−rnorm(100,5,1.1)
x3

Output

[1] 5.642503 5.230962 3.465324 4.893007 5.389745 5.130625 6.298657 5.127622
[9] 4.661126 3.961675 5.700836 5.052379 5.335749 5.687224 3.988179 6.265202
[17] 2.574791 5.020560 3.063921 4.538074 5.845454 4.441141 5.132403 5.392912
[25] 4.199037 4.928813 3.790512 5.729748 6.563736 3.527240 4.918002 5.072285
[33] 5.046033 7.535285 4.841451 5.285243 4.454778 5.104764 5.892299 4.777694
[41] 6.236553 4.273940 4.061579 4.243717 4.718178 4.136313 6.205208 3.945021
[49] 5.573720 3.991472 5.448390 3.880832 5.417734 5.198035 4.379759 3.015312
[57] 5.656754 4.730319 4.582328 4.613071 6.431624 4.570875 6.502455 6.362682
[65] 4.105208 3.955992 6.298363 5.305739 5.752251 4.152387 3.685942 5.001143
[73] 4.895666 5.040874 5.049020 5.750919 4.723756 4.280671 4.348718 4.981214
[81] 2.924487 5.078067 5.131206 6.301854 5.105356 3.125473 6.770442 3.368280
[89] 5.438926 3.709470 4.430937 4.175861 7.604855 5.325990 6.279979 5.076079
[97] 3.308669 6.649587 8.070356 5.078593

Example

floor(x3*10)/10

Output

[1] 5.6 5.2 3.4 4.8 5.3 5.1 6.2 5.1 4.6 3.9 5.7 5.0 5.3 5.6 3.9 6.2 2.5 5.0
[19] 3.0 4.5 5.8 4.4 5.1 5.3 4.1 4.9 3.7 5.7 6.5 3.5 4.9 5.0 5.0 7.5 4.8 5.2
[37] 4.4 5.1 5.8 4.7 6.2 4.2 4.0 4.2 4.7 4.1 6.2 3.9 5.5 3.9 5.4 3.8 5.4 5.1
[55] 4.3 3.0 5.6 4.7 4.5 4.6 6.4 4.5 6.5 6.3 4.1 3.9 6.2 5.3 5.7 4.1 3.6 5.0
[73] 4.8 5.0 5.0 5.7 4.7 4.2 4.3 4.9 2.9 5.0 5.1 6.3 5.1 3.1 6.7 3.3 5.4 3.7
[91] 4.4 4.1 7.6 5.3 6.2 5.0 3.3 6.6 8.0 5.0

Example4

 Live Demo

x4<−runif(100,2,10)
x4

Output

[1] 6.538808 6.545301 3.835959 8.054628 6.608576 9.910128 8.362813 7.586518
[9] 7.943493 7.505711 3.335142 4.091882 9.723603 5.839466 8.884706 6.643211
[17] 3.478755 6.083539 9.769651 4.829378 9.088905 3.168320 7.366497 6.269922
[25] 9.254521 6.945593 5.668853 9.016039 2.495471 7.628513 2.896996 7.785337
[33] 7.766262 2.443361 5.179641 4.008066 7.348657 2.744335 9.372329 5.142811
[41] 2.290931 9.818736 7.648366 4.074540 8.856767 9.181061 2.529208 6.189395
[49] 2.908754 5.087787 8.965813 4.811136 5.740659 4.255490 4.803441 2.255174
[57] 9.769002 8.929364 4.942824 2.755662 6.349775 2.314670 8.221101 9.565006
[65] 9.035085 7.787345 6.732919 2.133845 9.487212 4.278915 7.723010 3.097007
[73] 8.554924 7.757824 9.573348 9.389591 6.271267 7.636842 2.107888 5.260317
[81] 3.756438 3.833873 5.012878 4.322938 8.414654 8.491856 8.479793 7.557254
[89] 3.338573 2.760560 6.813499 9.596506 2.750367 7.317738 6.151885 4.906555
[97] 8.680140 3.040711 3.920693 7.318239

Example

floor(x4*10)/10

Output

[1] 6.5 6.5 3.8 8.0 6.6 9.9 8.3 7.5 7.9 7.5 3.3 4.0 9.7 5.8 8.8 6.6 3.4 6.0
[19] 9.7 4.8 9.0 3.1 7.3 6.2 9.2 6.9 5.6 9.0 2.4 7.6 2.8 7.7 7.7 2.4 5.1 4.0
[37] 7.3 2.7 9.3 5.1 2.2 9.8 7.6 4.0 8.8 9.1 2.5 6.1 2.9 5.0 8.9 4.8 5.7 4.2
[55] 4.8 2.2 9.7 8.9 4.9 2.7 6.3 2.3 8.2 9.5 9.0 7.7 6.7 2.1 9.4 4.2 7.7 3.0
[73] 8.5 7.7 9.5 9.3 6.2 7.6 2.1 5.2 3.7 3.8 5.0 4.3 8.4 8.4 8.4 7.5 3.3 2.7
[91] 6.8 9.5 2.7 7.3 6.1 4.9 8.6 3.0 3.9 7.3

Example5

 Live Demo

x5<−rexp(100)
x5

Output

[1] 0.3693550956 4.4835554843 0.1593435327 1.5926173105 0.3622606342
[6] 0.4774536472 0.3866934655 0.0551065494 0.5390452691 2.3621970472
[11] 0.8336312581 1.0139794843 0.8100316476 0.9223797302 1.5480979568
[16] 0.2938991212 2.1378652504 0.0157313182 0.3185261055 0.1638578176
[21] 0.1943867127 0.8233978414 0.1332402905 0.2755519945 1.3449708027
[26] 0.6198626405 0.2668860275 0.2326414920 1.3924008227 1.0283894147
[31] 0.5750101595 0.6337804292 0.3722617605 2.8044816875 0.6452543922
[36] 0.5045517669 0.0295312004 0.6874382785 0.0276178052 0.5838950933
[41] 0.4059721236 0.7831552615 0.9100687967 0.2333946077 0.0490050911
[46] 1.0237078620 0.4145099246 1.2310644500 1.0741089529 0.8645681506
[51] 0.0524017420 0.9652211631 0.8568648770 0.6978872763 3.9212165380
[56] 0.2542064069 0.0226820768 1.8359337756 1.8138077780 2.5640936853
[61] 0.0122036459 1.8706417232 0.0472460319 1.2739779510 0.6243003448
[66] 0.4667825093 0.0919807909 0.4396443103 3.8503847623 0.2734695016
[71] 0.1047210614 1.9733380600 0.0616727980 0.5205048658 2.7171700987
[76] 3.2596589772 1.2333627948 0.0007526749 0.2288445337 0.9081312192
[81] 1.4141255180 0.3164622360 0.9008373019 3.0270216091 0.2559199218
[86] 0.2828875592 0.0460792963 0.0178526601 0.5076802722 1.4419499970
[91] 0.3998881704 0.5913354810 0.3965095463 1.5288099257 0.7145151570
[96] 1.0150692239 1.8374356142 0.6863701791 5.5953387537 1.2190533842

Example

floor(x5*10)/10

Output

[1] 0.3 4.4 0.1 1.5 0.3 0.4 0.3 0.0 0.5 2.3 0.8 1.0 0.8 0.9 1.5 0.2 2.1 0.0
[19] 0.3 0.1 0.1 0.8 0.1 0.2 1.3 0.6 0.2 0.2 1.3 1.0 0.5 0.6 0.3 2.8 0.6 0.5
[37] 0.0 0.6 0.0 0.5 0.4 0.7 0.9 0.2 0.0 1.0 0.4 1.2 1.0 0.8 0.0 0.9 0.8 0.6
[55] 3.9 0.2 0.0 1.8 1.8 2.5 0.0 1.8 0.0 1.2 0.6 0.4 0.0 0.4 3.8 0.2 0.1 1.9
[73] 0.0 0.5 2.7 3.2 1.2 0.0 0.2 0.9 1.4 0.3 0.9 3.0 0.2 0.2 0.0 0.0 0.5 1.4
[91] 0.3 0.5 0.3 1.5 0.7 1.0 1.8 0.6 5.5 1.2
raja
Published on 09-Feb-2021 12:21:48
Advertisements