- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to generate random samples rounded to 4 decimal places in R?
Random samples can be generated in many ways such as using discrete and continuous distributions, using integer vectors, using numerical vectors, using character vectors and/or factor vectors, also with columns of a data set. If we have the sample that is continuous in nature then the values are likely to contain many values after decimal point and we can limit those values to 4 or use any other limit using round function.
Example
x1<-round(rnorm(10),4) x1
Output
[1] 0.1667 -0.5536 -0.2748 -0.1064 -0.7740 0.0009 0.3966 -0.2256 -0.4090 [10] 0.4069
Example
x2<-round(rnorm(50,545,18.5),4) x2
Output
[1] 534.2164 562.2330 508.1712 544.9145 568.2658 566.7066 520.1372 555.8063 [9] 490.2302 530.0890 550.1564 552.7664 512.8081 539.7952 536.8150 527.6695 [17] 543.5097 556.8944 528.4357 519.7085 554.9833 541.6425 580.0051 559.1235 [25] 558.1846 549.6800 581.8289 555.0069 560.3540 528.3715 523.2465 526.7506 [33] 513.3090 569.5984 504.2076 565.5712 594.9353 551.5697 550.7176 560.9044 [41] 553.6006 548.2351 571.0769 513.0627 560.5451 529.1586 552.1659 541.0974 [49] 557.9535 535.8345
Example
x3<-round(runif(100,2,10),4) x3
Output
[1] 3.3254 4.2719 2.5700 6.6529 2.3641 5.7524 3.5321 4.0652 8.7108 9.5321 [11] 4.8742 2.6012 5.9765 4.1423 6.1051 2.3037 3.0290 2.2835 4.3078 6.9569 [21] 3.0007 9.8222 7.3561 7.8823 6.9002 9.8261 3.9888 9.4723 7.6115 3.5430 [31] 3.5425 2.5960 6.5738 6.5245 4.6534 5.0535 2.4721 9.9349 3.3694 2.1342 [41] 9.8047 3.2214 4.1357 5.9450 7.5481 9.9704 9.1236 6.6892 4.2183 4.2098 [51] 9.1609 3.3085 3.8390 5.6355 3.2058 3.1597 9.0767 4.7739 3.4916 6.7490 [61] 8.5052 3.1381 4.4015 2.5416 9.0075 8.8939 4.2335 7.5611 4.3264 6.2221 [71] 2.6467 3.4608 3.3156 6.6610 9.0668 7.0933 9.2373 6.7479 6.3007 9.3724 [81] 5.5539 9.2073 3.7334 2.8943 4.1278 6.9277 4.0263 7.8080 3.2616 5.0656 [91] 4.7286 9.8659 4.3485 5.7590 4.8930 5.5345 3.5390 9.2349 2.0937 6.1323
Example
x4<-round(rexp(100,3.5),4) x4
Output
[1] 0.0671 0.1094 0.0880 0.0271 0.0188 0.0594 0.9127 0.4143 0.2443 0.1643 [11] 0.2951 0.0381 0.1241 0.3050 0.1466 0.3556 0.1066 1.0008 0.0905 0.0861 [21] 0.2225 0.0188 0.8789 0.1757 0.4530 0.0884 0.2754 0.2594 0.2853 0.2232 [31] 0.1717 0.3329 0.5694 0.1007 1.5399 0.3020 0.1537 0.0227 0.3811 0.1305 [41] 0.2021 0.0148 0.1565 0.3016 0.2054 0.0474 0.4245 0.2189 0.0746 0.0540 [51] 0.1479 0.1197 0.0670 0.5775 0.4953 0.9968 0.1305 0.2396 0.1523 0.0392 [61] 0.2877 0.0349 0.1656 0.2726 0.4670 0.2336 0.0932 0.2731 0.3326 0.0030 [71] 0.3336 0.5345 0.1917 0.3915 0.6645 0.0015 0.3518 0.7542 0.0054 0.2730 [81] 0.5271 0.0723 0.0439 0.2569 0.4438 1.0872 0.1163 0.5161 0.0244 0.0762 [91] 0.1537 0.0876 0.2585 0.0544 0.1379 0.3796 0.1431 0.2645 0.5104 0.3769
Example
x5<-round(rexp(100,10),4) x5
Output
[1] 0.0389 0.0984 0.0092 0.0384 0.1462 0.2182 0.2277 0.0102 0.0589 0.0148 [11] 0.0952 0.1037 0.6899 0.0279 0.0562 0.0555 0.3258 0.0199 0.0649 0.0214 [21] 0.1385 0.0805 0.0707 0.0129 0.0227 0.0688 0.0681 0.4176 0.2576 0.4757 [31] 0.0199 0.1240 0.0096 0.0349 0.0394 0.0025 0.0233 0.0495 0.0408 0.1025 [41] 0.0480 0.2670 0.0988 0.1490 0.0547 0.0152 0.0390 0.0286 0.0113 0.0384 [51] 0.1952 0.0361 0.0834 0.0852 0.0246 0.0099 0.1016 0.0028 0.0340 0.3052 [61] 0.1962 0.1240 0.0688 0.0276 0.0665 0.1007 0.1076 0.0231 0.0344 0.0634 [71] 0.1471 0.0151 0.0515 0.2178 0.1477 0.0004 0.0597 0.1680 0.1433 0.0205 [81] 0.0958 0.1006 0.0436 0.0225 0.2576 0.0816 0.2283 0.0376 0.0981 0.0226 [91] 0.0263 0.0126 0.0322 0.0307 0.1616 0.0353 0.0021 0.0073 0.1099 0.0533
Example
x6<-round(rnorm(100,5,1),4) x6
Output
[1] 5.6763 6.0442 3.8460 6.9212 5.8802 4.5369 5.7293 5.4419 4.8554 4.8552 [11] 6.3626 3.0416 5.5835 3.3639 3.5631 4.4367 6.1987 6.5645 4.4211 4.5338 [21] 5.1488 2.3234 6.7397 4.6025 4.6126 4.3689 5.3708 6.2197 4.7341 5.7758 [31] 3.7592 6.2968 5.8481 4.9489 4.8234 4.9166 3.5623 4.4833 3.4248 5.2370 [41] 5.0679 6.2172 4.6400 5.7530 6.9294 5.3914 5.2008 3.8987 5.8946 3.6129 [51] 3.6497 5.5797 3.7814 4.9368 4.0392 5.5592 3.1513 5.3439 5.1348 4.6515 [61] 5.3077 4.8295 3.8849 4.8448 3.3433 3.2978 4.0765 7.9617 5.1990 4.1103 [71] 4.5910 4.3942 6.5974 6.1603 4.2415 4.2938 7.0490 4.5537 4.9323 5.6792 [81] 6.3582 4.3443 4.0635 4.3988 7.5974 5.1550 6.2474 4.1624 4.0028 4.1772 [91] 3.7348 5.1781 5.4563 4.9231 6.7859 3.9984 4.8556 4.1362 5.0205 6.1024
Example
x7<-round(rt(100,35),4) x7
Output
[1] 0.3204 -1.3065 -1.0071 -2.0756 -0.0839 0.7791 -0.0810 -1.5429 0.6007 [10] -0.3293 1.4005 -2.2854 -2.9662 -0.1180 1.0510 -0.0574 -0.5469 -1.1538 [19] -0.0302 0.3048 1.3370 1.3374 2.1286 -1.0568 0.1238 -0.2602 -1.4643 [28] 0.9552 -0.0340 -0.3254 -2.4275 -0.4864 2.8389 -1.0984 -1.0615 -2.2571 [37] -1.7641 -1.0414 -0.8122 -0.9599 1.0389 -2.0130 1.4173 0.6212 -0.2002 [46] 1.0792 0.8220 -0.4070 -0.4462 0.7189 1.7046 0.5914 -0.2832 0.2117 [55] 0.0152 0.9239 0.1632 -0.0780 -1.4047 1.0794 -0.8982 0.9582 -0.2948 [64] 0.3619 1.8029 -1.7045 -0.0908 1.6610 1.1330 -0.9057 1.6303 1.0317 [73] 0.9091 0.7074 0.5091 -0.0232 0.2435 1.4325 0.4925 0.6357 -1.3657 [82] 1.4857 0.5618 0.1661 -0.1686 -0.3427 -1.1928 -1.4164 -0.4323 0.2180 [91] 1.2018 -2.5645 1.5959 0.1095 -0.2138 -0.3220 -0.2515 1.5408 -1.7223 [100] 0.9292
Example
x8<-round(rf(100,2,36),4) x8
Output
[1] 0.8147 0.0376 0.1377 0.6935 2.3744 1.8458 0.2076 0.7661 2.1681 2.1221 [11] 0.2457 0.2045 2.8954 0.5044 0.3615 0.8056 1.0633 0.6306 1.1108 0.2749 [21] 0.2724 0.0587 1.8507 1.2245 0.7484 0.1565 0.3563 0.9754 1.1266 0.2427 [31] 0.1566 0.5620 1.0601 0.2009 6.3452 0.1189 1.5286 1.0583 1.1904 0.8433 [41] 0.4249 0.0324 0.0383 1.2045 1.7232 2.3048 3.0217 0.2014 0.3035 0.3601 [51] 0.4877 0.1151 0.2056 0.6065 0.9192 0.6845 3.7313 0.0209 1.3406 0.1404 [61] 0.7136 1.3781 0.0560 2.8054 1.3838 3.4458 0.3417 0.1298 1.3320 0.0430 [71] 2.5704 3.4338 0.8516 1.3397 1.5941 3.6932 0.1111 1.1202 1.7724 0.8752 [81] 3.8412 0.2798 0.4708 2.1400 0.8470 0.5233 0.7540 0.2193 0.1412 1.8143 [91] 0.9365 0.3903 2.3025 1.6087 0.2255 0.5314 1.8405 0.2321 0.8266 1.3261
Example
x9<-round(rlnorm(100,5,2),4) x9
Output
[1] 3663.4697 1177.2276 11.4535 815.7052 3066.3002 63.3265 [7] 6.8847 35.7552 236.8196 79.1767 689.2404 5.5008 [13] 344.2535 1138.8724 287.1132 49.1381 489.8662 158.1743 [19] 109.4748 355.1509 855.8181 191.1866 9.0679 314.2612 [25] 1008.5422 17.5288 20.0675 37.5216 2466.2132 1104.7948 [31] 570.9207 11.2588 3.1528 481.2571 354.3538 4.6479 [37] 928.3763 743.7015 1054.3399 253.3067 20.7045 352.9785 [43] 1849.3159 167.5804 0.9107 2489.4271 12994.5896 1119.0242 [49] 24.1053 1861.5244 754.3061 28.5280 24.2137 57.0343 [55] 203.8550 245.9501 582.4504 7571.2720 305.8121 3.3545 [61] 10962.3609 35055.4310 345.9599 50.3923 815.9937 0.1470 [67] 142.1961 310.8576 632.7251 929.2929 61.2483 166.1665 [73] 238.8496 5.8300 252.5437 93.1759 4494.1969 40.7933 [79] 2271.2802 743.6349 387.8769 2.0409 65.9566 64.4320 [85] 66.8955 1093.2168 24.1452 65.7903 31.2941 9.4564 [91] 5453.9356 27.8865 72.1684 216.2117 46.6497 72.9932 [97] 12.9700 217.6623 2230.6189 329.4752
- Related Articles
- How to generate random samples from different statistical distributions using parameters as a list in R?
- How to generate Bernoulli random variable in R?
- How to generate standard normal random numbers in R?
- How to display Y-axis labels with more decimal places in R?
- How to format all decimal places in an R vector and data frame?
- How to round correlation values in the correlation matrix to zero decimal places in R?
- How to truncate a numerical vector to a specified number of decimal places in R?
- How to limit Decimal Places in Android EditText?
- How to generate random colors in Matplotlib?
- Visualise \( 4 . \overline{26} \) on the number line, up to 4 decimal places.
- How to format number to 2 decimal places in MySQL?
- How to generate a random number in C++?
- How to generate large random numbers in Java?
- How to generate a random number in JavaScript?
- How to round a number to n decimal places in Java

Advertisements