How to truncate a numerical vector to a specified number of decimal places in R?

R ProgrammingServer Side ProgrammingProgramming

The truncation means removing the number of decimal places but not rounding. For example, if we have a value 5.1742145 then truncating to one decimal place will be 5.1 and rounding will be 5.2. In R, we can do this by using trunc function as shown in the below examples.

Example

 Live Demo

x1<-rnorm(10)
x1

Output

[1] -0.84499092 -0.54565852 2.41693972 -0.94680186 -0.03660552 -0.54338429
[7] -1.74402331 0.58996861 0.70335363 -0.44926205

Example

trunc(x1*10^2)/10^2

Output

[1] -0.84 -0.54 2.41 -0.94 -0.03 -0.54 -1.74 0.58 0.70 -0.44

Example

trunc(x1*10^3)/10^3

Output

[1] -0.844 -0.545 2.416 -0.946 -0.036 -0.543 -1.744 0.589 0.703 -0.449

Example

 Live Demo

x2<-rnorm(80,1,0.98)
x2

Output

[1] 0.73523335 0.26091423 0.88429939 1.78455144 1.79656595 1.25686211
[7] 1.39409479 1.95765673 1.60515780 1.46417092 1.07822113 -0.04666546
[13] 1.31617059 1.64466226 2.21541252 1.45431877 1.29660778 0.17819482
[19] 0.79799969 2.28867356 0.40601640 1.75875402 1.00621828 0.78295772
[25] 0.74065368 0.46327219 0.92944349 1.50652050 -0.59017641 3.11837571
[31] 0.68041176 1.54054947 0.90897416 0.14948413 2.41438381 2.69803352
[37] 1.64154794 0.26402773 0.00709498 1.03914237 0.50448671 0.69754999
[43] 1.13548938 2.38301247 1.34286979 0.48734357 0.81482907 1.40386607
[49] 2.77024439 0.72304358 1.04188201 -0.85375466 0.29898633 2.94726645
[55] 1.78984195 2.44087099 0.26813148 1.39159847 3.13727521 0.75036218
[61] -0.21608581 1.01517297 0.09417932 -0.45515058 1.98327879 -0.15034965
[67] -0.49404002 0.57145033 1.23275717 0.75189987 0.50521022 1.01771721
[73] 0.34640539 0.96182063 -0.42281236 -0.92742950 1.09788222 3.81259149
[79] 0.85069551 0.31154613

Example

trunc(x2*10^4)/10^4

Output

[1] 0.7352 0.2609 0.8842 1.7845 1.7965 1.2568 1.3940 1.9576 1.6051
[10] 1.4641 1.0782 -0.0466 1.3161 1.6446 2.2154 1.4543 1.2966 0.1781
[19] 0.7979 2.2886 0.4060 1.7587 1.0062 0.7829 0.7406 0.4632 0.9294
[28] 1.5065 -0.5901 3.1183 0.6804 1.5405 0.9089 0.1494 2.4143 2.6980
[37] 1.6415 0.2640 0.0070 1.0391 0.5044 0.6975 1.1354 2.3830 1.3428
[46] 0.4873 0.8148 1.4038 2.7702 0.7230 1.0418 -0.8537 0.2989 2.9472
[55] 1.7898 2.4408 0.2681 1.3915 3.1372 0.7503 -0.2160 1.0151 0.0941
[64] -0.4551 1.9832 -0.1503 -0.4940 0.5714 1.2327 0.7518 0.5052 1.0177
[73] 0.3464 0.9618 -0.4228 -0.9274 1.0978 3.8125 0.8506 0.3115

Example

 Live Demo

x3<-rexp(100,1.27)
x3

Output

[1] 0.97354729 0.20103472 1.63167244 0.02460948 0.01444584 0.04192671
[7] 0.43062257 2.12977561 0.57762242 0.08040930 0.12102343 0.33892249
[13] 0.91513872 0.50518128 0.51161536 0.39958359 0.57104344 1.58178116
[19] 0.02408988 1.08273852 4.01646829 0.87133101 0.01001515 0.29375628
[25] 0.07013194 2.32737583 0.22341190 0.23485741 0.18605451 1.66485943
[31] 1.76461071 0.75302518 0.26754955 0.82001534 0.67100698 1.51657645
[37] 0.11736558 0.40746685 0.70611805 0.28263227 0.79404863 0.63230590
[43] 0.40523074 1.51551600 0.49647246 0.06129056 0.28417023 0.55119749
[49] 0.64287599 2.23207622 0.22973841 3.05214082 1.36478611 0.73737631
[55] 2.21655371 0.09223953 0.85371208 0.20931428 0.04644229 0.69783563
[61] 0.91277564 2.04319940 2.08419002 0.77866366 1.59369459 3.77805372
[67] 1.46403803 3.31471381 0.95154415 0.04852725 1.16589379 0.21751345
[73] 1.79418557 0.17208061 0.12701123 0.65970569 2.61858576 0.07995897
[79] 0.51086852 0.73025662 1.57320588 1.09163136 0.31792746 0.03298141
[85] 1.51342419 0.46400639 1.07054594 0.48181990 1.25250106 0.55155349
[91] 0.39274532 1.33788105 0.35586535 0.57552629 0.49346111 1.13563698
[97] 1.94845460 0.82386377 2.44021767 0.05184776

Example

trunc(x3*10^4)/10^4

Output

[1] 0.9735 0.2010 1.6316 0.0246 0.0144 0.0419 0.4306 2.1297 0.5776 0.0804
[11] 0.1210 0.3389 0.9151 0.5051 0.5116 0.3995 0.5710 1.5817 0.0240 1.0827
[21] 4.0164 0.8713 0.0100 0.2937 0.0701 2.3273 0.2234 0.2348 0.1860 1.6648
[31] 1.7646 0.7530 0.2675 0.8200 0.6710 1.5165 0.1173 0.4074 0.7061 0.2826
[41] 0.7940 0.6323 0.4052 1.5155 0.4964 0.0612 0.2841 0.5511 0.6428 2.2320
[51] 0.2297 3.0521 1.3647 0.7373 2.2165 0.0922 0.8537 0.2093 0.0464 0.6978
[61] 0.9127 2.0431 2.0841 0.7786 1.5936 3.7780 1.4640 3.3147 0.9515 0.0485
[71] 1.1658 0.2175 1.7941 0.1720 0.1270 0.6597 2.6185 0.0799 0.5108 0.7302
[81] 1.5732 1.0916 0.3179 0.0329 1.5134 0.4640 1.0705 0.4818 1.2525 0.5515
[91] 0.3927 1.3378 0.3558 0.5755 0.4934 1.1356 1.9484 0.8238 2.4402 0.0518

Example

trunc(x3*10^2)/10^2

Output

[1] 0.97 0.20 1.63 0.02 0.01 0.04 0.43 2.12 0.57 0.08 0.12 0.33 0.91 0.50 0.51
[16] 0.39 0.57 1.58 0.02 1.08 4.01 0.87 0.01 0.29 0.07 2.32 0.22 0.23 0.18 1.66
[31] 1.76 0.75 0.26 0.82 0.67 1.51 0.11 0.40 0.70 0.28 0.79 0.63 0.40 1.51 0.49
[46] 0.06 0.28 0.55 0.64 2.23 0.22 3.05 1.36 0.73 2.21 0.09 0.85 0.20 0.04 0.69
[61] 0.91 2.04 2.08 0.77 1.59 3.77 1.46 3.31 0.95 0.04 1.16 0.21 1.79 0.17 0.12
[76] 0.65 2.61 0.07 0.51 0.73 1.57 1.09 0.31 0.03 1.51 0.46 1.07 0.48 1.25 0.55
[91] 0.39 1.33 0.35 0.57 0.49 1.13 1.94 0.82 2.44 0.05

Example

trunc(x3*10)/10

Output

[1] 0.9 0.2 1.6 0.0 0.0 0.0 0.4 2.1 0.5 0.0 0.1 0.3 0.9 0.5 0.5 0.3 0.5 1.5
[19] 0.0 1.0 4.0 0.8 0.0 0.2 0.0 2.3 0.2 0.2 0.1 1.6 1.7 0.7 0.2 0.8 0.6 1.5
[37] 0.1 0.4 0.7 0.2 0.7 0.6 0.4 1.5 0.4 0.0 0.2 0.5 0.6 2.2 0.2 3.0 1.3 0.7
[55] 2.2 0.0 0.8 0.2 0.0 0.6 0.9 2.0 2.0 0.7 1.5 3.7 1.4 3.3 0.9 0.0 1.1 0.2
[73] 1.7 0.1 0.1 0.6 2.6 0.0 0.5 0.7 1.5 1.0 0.3 0.0 1.5 0.4 1.0 0.4 1.2 0.5
[91] 0.3 1.3 0.3 0.5 0.4 1.1 1.9 0.8 2.4 0.0

Example

trunc(x3*10^5)/10^5

Output

[1] 0.97354 0.20103 1.63167 0.02460 0.01444 0.04192 0.43062 2.12977 0.57762
[10] 0.08040 0.12102 0.33892 0.91513 0.50518 0.51161 0.39958 0.57104 1.58178
[19] 0.02408 1.08273 4.01646 0.87133 0.01001 0.29375 0.07013 2.32737 0.22341
[28] 0.23485 0.18605 1.66485 1.76461 0.75302 0.26754 0.82001 0.67100 1.51657
[37] 0.11736 0.40746 0.70611 0.28263 0.79404 0.63230 0.40523 1.51551 0.49647
[46] 0.06129 0.28417 0.55119 0.64287 2.23207 0.22973 3.05214 1.36478 0.73737
[55] 2.21655 0.09223 0.85371 0.20931 0.04644 0.69783 0.91277 2.04319 2.08419
[64] 0.77866 1.59369 3.77805 1.46403 3.31471 0.95154 0.04852 1.16589 0.21751
[73] 1.79418 0.17208 0.12701 0.65970 2.61858 0.07995 0.51086 0.73025 1.57320
[82] 1.09163 0.31792 0.03298 1.51342 0.46400 1.07054 0.48181 1.25250 0.55155
[91] 0.39274 1.33788 0.35586 0.57552 0.49346 1.13563 1.94845 0.82386 2.44021
[100] 0.05184

Example

 Live Demo

x4<-runif(80,2,7)
x4

Output

[1] 2.772394 6.738541 3.078799 2.742822 3.884845 6.522373 2.154879 6.924090
[9] 4.698355 3.496807 6.450028 3.237964 2.140272 4.935724 5.799457 5.868347
[17] 3.317251 5.721810 2.556979 5.841216 6.455976 4.572611 3.344829 3.307405
[25] 3.489913 3.791528 2.262266 3.473043 5.674577 2.634500 3.883398 5.052654
[33] 5.519941 4.555885 2.265511 2.408007 2.009196 4.378668 6.557588 5.960275
[41] 6.338667 3.153885 3.265560 4.952539 5.006222 5.792131 3.001510 3.340512
[49] 6.177294 5.031953 5.421668 3.060252 2.902759 6.096917 6.006954 5.519656
[57] 6.041697 6.084179 2.544475 5.540480 4.098558 6.239475 2.123080 3.714456
[65] 3.042817 6.845781 2.273269 5.203946 3.126360 3.388676 2.018071 3.354915
[73] 4.220318 3.435676 3.541337 5.988462 2.947339 3.921962 6.088544 4.470260

Example

trunc(x4*10^2)/10^2

Output

[1] 2.77 6.73 3.07 2.74 3.88 6.52 2.15 6.92 4.69 3.49 6.45 3.23 2.14 4.93 5.79
[16] 5.86 3.31 5.72 2.55 5.84 6.45 4.57 3.34 3.30 3.48 3.79 2.26 3.47 5.67 2.63
[31] 3.88 5.05 5.51 4.55 2.26 2.40 2.00 4.37 6.55 5.96 6.33 3.15 3.26 4.95 5.00
[46] 5.79 3.00 3.34 6.17 5.03 5.42 3.06 2.90 6.09 6.00 5.51 6.04 6.08 2.54 5.54
[61] 4.09 6.23 2.12 3.71 3.04 6.84 2.27 5.20 3.12 3.38 2.01 3.35 4.22 3.43 3.54
[76] 5.98 2.94 3.92 6.08 4.47

Example

trunc(x4*10^4)/10^4

Output

[1] 2.7723 6.7385 3.0787 2.7428 3.8848 6.5223 2.1548 6.9240 4.6983 3.4968
[11] 6.4500 3.2379 2.1402 4.9357 5.7994 5.8683 3.3172 5.7218 2.5569 5.8412
[21] 6.4559 4.5726 3.3448 3.3074 3.4899 3.7915 2.2622 3.4730 5.6745 2.6345
[31] 3.8833 5.0526 5.5199 4.5558 2.2655 2.4080 2.0091 4.3786 6.5575 5.9602
[41] 6.3386 3.1538 3.2655 4.9525 5.0062 5.7921 3.0015 3.3405 6.1772 5.0319
[51] 5.4216 3.0602 2.9027 6.0969 6.0069 5.5196 6.0416 6.0841 2.5444 5.5404
[61] 4.0985 6.2394 2.1230 3.7144 3.0428 6.8457 2.2732 5.2039 3.1263 3.3886
[71] 2.0180 3.3549 4.2203 3.4356 3.5413 5.9884 2.9473 3.9219 6.0885 4.4702

Example

 Live Demo

x5<-rnorm(50,5,2.1)
x5

Output

[1] -0.3385029 3.5081981 3.2785845 4.3608520 8.0744767 5.1443592
[7] 6.2600382 2.8735485 4.9951295 5.2179696 3.9008037 3.8242194
[13] 2.2699006 3.8534745 8.2748792 5.4031592 5.1962031 7.6637385
[19] 5.6372086 5.0332861 8.6463522 7.2675736 6.1497623 8.2789534
[25] 6.9992113 5.6322308 6.7657675 5.3396055 4.5774547 6.9155615
[31] 2.6551406 7.0196728 1.6628165 12.2098125 3.3934465 7.9297134
[37] 1.1623610 7.0235596 7.6934001 5.2798322 4.4403453 8.6678835
[43] 6.1327257 4.2250337 5.6078953 2.9696670 6.2362681 5.8023505
[49] 3.2335220 7.1395182

Example

trunc(x5*10^2)/10^2

Output

[1] -0.33 3.50 3.27 4.36 8.07 5.14 6.26 2.87 4.99 5.21 3.90 3.82
[13] 2.26 3.85 8.27 5.40 5.19 7.66 5.63 5.03 8.64 7.26 6.14 8.27
[25] 6.99 5.63 6.76 5.33 4.57 6.91 2.65 7.01 1.66 12.20 3.39 7.92
[37] 1.16 7.02 7.69 5.27 4.44 8.66 6.13 4.22 5.60 2.96 6.23 5.80
[49] 3.23 7.13
raja
Published on 07-Dec-2020 05:10:40
Advertisements