# How to generate a normal random vector using the mean of a vector in R?

To create a normal random vector, we can use rnorm function with mean and standard deviation as well as without passing these arguments. If we have a different vector derived from another distribution or simply represent some numbers then we can use the mean of that vector in the rnorm function for mean argument.

## Example

Live Demo

set.seed(101)
x1<-rpois(100,5)
x1

## Output

[1] 4 2 6 6 3 4 5 4 6 5 8 6 6 8 5 5 7 3 4 1 6 9 3 6 8
[26] 7 2 4 4 6 4 4 3 3 5 8 3 7 1 8 4 5 6 4 4 3 2 8 7 2
[51] 2 4 6 6 5 7 6 5 6 5 7 7 5 11 7 6 1 4 8 9 3 9 5 7 7
[76] 4 2 3 6 9 6 7 7 10 1 9 4 4 3 1 6 8 6 3 3 7 4 3 2 2

## Example

Sample1<-rnorm(50,mean(x1))
Sample1

## Output

[1] 3.969745 4.845529 5.697901 3.723097 5.869058 4.068813 5.285381 6.249809
[9] 6.293722 4.692137 4.860198 3.708827 4.478642 5.232458 5.542604 5.506835
[17] 4.432202 5.268902 5.062350 5.045177 6.629897 6.739937 6.273158 5.042396
[25] 3.301065 4.082555 5.422492 3.842054 5.258339 5.069016 6.972148 6.231675
[33] 4.608625 4.576119 3.391073 5.590750 5.125387 6.468046 5.844097 6.672549
[41] 6.445470 5.085735 4.758987 4.399835 5.402015 4.329474 4.675095 6.484993
[49] 5.617454 4.305604

## Example

Live Demo

x2<-runif(40,2,5)
x2

## Output

[1] 3.236019 2.943532 4.886012 4.174843 4.094289 3.075312 2.827839 4.304467
[9] 2.326852 4.612022 2.986805 4.847419 3.605988 4.678886 4.022470 2.406397
[17] 4.327018 3.053227 3.050786 3.685067 3.890822 4.389564 2.951477 3.848331
[25] 4.401844 3.014718 2.465123 2.022005 2.403867 4.456938 4.659330 4.275964
[33] 4.288265 4.511266 2.694515 3.961114 2.702145 2.339766 4.894741 2.344851

## Example

Sample2<-rnorm(50,mean(x2))
Sample2

## Output

[1] 2.7645665 2.6537541 1.4076983 2.8925864 3.8609001 2.6550045 2.6722310
[8] 3.1268140 5.0901382 2.1818420 2.6430374 3.6710024 3.2822846 1.8175404
[15] 4.3777518 1.6070401 5.2098435 5.3133112 4.9168906 3.0455723 2.5167968
[22] 3.0880777 2.0142883 3.6437147 4.0962980 3.1383124 3.8954753 2.9561004
[29] 3.1869059 2.9787093 2.8073421 3.6791349 2.6728036 4.1584487 3.0235851
[36] 0.6031717 3.8891456 5.5590415 3.1556851 3.6746972 3.4642883 3.8202408
[43] 1.9220866 1.8392810 2.4990537 4.2023688 2.6454874 2.1476048 3.4247437
[50] 1.5751937

## Example

Live Demo

x3<-rexp(40,3.5)
x3

## Output

[1] 0.340070611 0.060874495 0.437249688 0.451933591 0.501142156 0.121902693
[7] 0.164040205 0.910057958 0.058536057 0.144731409 0.470393085 0.487080705
[13] 0.164886282 0.254101261 0.465869002 0.006060842 0.002589951 0.290490088
[19] 0.107732895 0.190234003 0.055386172 0.046035375 0.316864801 0.164904071
[25] 0.332742858 0.083107876 0.012238640 0.635069982 0.035221374 0.674091355
[31] 0.113435488 1.376133524 0.326500018 0.264186670 0.130493013 0.293179982
[37] 0.954143275 0.078153099 0.346049862 0.081041369

## Example

Sample3<-rnorm(50,mean(x3))
Sample3

## Output

[1] 0.82932733 0.43281739 -0.73718085 0.75001763 -0.27458596 1.08118060
[7] -0.65670267 0.70135351 0.98586218 -2.49283423 0.70378079 0.10228658
[13] 0.61340795 0.95991848 0.27352495 -1.11108685 0.18902158 -1.18286719
[19] 0.39360739 0.11334012 1.36230597 1.45772059 -0.29650409 1.59499517
[25] -0.81136649 0.86194355 0.54641710 1.54240538 1.18082602 3.09350532
[31] -0.44871342 -0.44068343 0.51672887 0.83014588 1.18188996 0.35880207
[37] -0.08949876 -0.01457706 0.20724582 -1.36114868 2.21332679 0.34850885
[43] 1.11953554 0.14462373 0.15050421 -0.79027087 1.49768851 -0.56139687
[49] 0.36863964 0.10356583

## Example

Live Demo

x4<-sample(0:9,100,replace=TRUE)
x4

## Output

[1] 0 1 8 1 3 5 0 5 6 8 2 6 0 7 6 2 4 6 3 1 3 5 5 0 7 2 6 9 0 1 5 7 2 0 3 4 2
[38] 4 7 2 0 2 9 3 4 4 7 3 7 9 3 9 8 8 6 4 4 4 8 3 7 2 9 6 3 3 9 4 6 3 0 5 3 0
[75] 4 4 5 2 5 6 5 8 8 6 3 5 6 1 0 0 5 7 5 8 8 4 1 6 2 9

## Example

Sample4<-rnorm(50,mean(x4))
Sample4

## Output

[1] 4.836175 3.963319 4.496714 5.130524 7.196743 5.152192 5.675204 3.859852
[9] 5.403791 3.860237 4.916302 5.928370 4.343460 4.875806 5.749838 3.623455
[17] 3.936333 2.705256 3.892078 4.226458 4.289463 2.518079 2.758168 5.049665
[25] 4.751905 3.784941 3.238820 4.634881 6.190992 3.850284 4.071455 4.349709
[33] 3.965399 5.825253 6.392003 4.545963 5.234481 3.671405 2.777317 3.708514
[41] 3.659098 3.774697 5.156098 6.222536 3.956885 4.573353 4.499367 5.852005
[49] 3.917576 3.444557

## Example

Live Demo

x5<-sample(1001:9999,100,replace=TRUE)
x5

## Output

[1] 9506 5004 1216 6114 4258 7686 3782 5827 4960 1054 2371 4178 3991 1668 9233
[16] 8990 8994 3284 9370 2713 4549 5747 2770 6757 6832 7635 1561 1659 2521 8928
[31] 8147 4741 9194 7724 7071 4758 5936 6833 3098 1596 6216 5024 6129 1785 8970
[46] 9457 2030 9202 9774 7865 7689 5187 9128 4633 4163 6979 2454 8299 1797 6964
[61] 2311 4397 4796 7164 5405 7905 6160 3061 6808 5207 4678 4937 6542 9713 8927
[76] 9087 8915 3996 9207 8813 1694 7151 2818 6583 3242 6414 7717 7532 7491 4300
[91] 2070 3141 2519 7315 9490 5158 8735 3187 7588 1760

## Example

Sample5<-rnorm(50,mean(x5))
Sample5

## Output

[1] 5835.694 5837.989 5837.878 5836.064 5838.504 5837.036 5836.654 5837.286
[9] 5836.040 5837.026 5837.058 5835.924 5837.195 5836.200 5835.307 5836.525
[17] 5837.052 5836.925 5836.167 5836.521 5837.745 5837.453 5835.681 5836.634
[25] 5837.442 5835.078 5836.910 5835.975 5836.866 5835.711 5836.928 5838.439
[33] 5837.929 5836.361 5835.677 5836.355 5835.905 5836.094 5834.243 5836.918
[41] 5837.966 5837.005 5838.369 5836.746 5837.065 5836.915 5836.037 5835.473
[49] 5837.818 5835.947

Updated on: 10-Oct-2020

750 Views