Doing statistics using R is perfect for many data analysts. Dealing with various statistical distributions and generating random numbers from some widely used distributions are mandatory for data science. In this post, we show how to generate random numbers from Normal, Uniform and Poisson distributions in R.
- Normal distribution
Normal variates can be generated by using rnorm() function in R. The basic form of the function is:
rnorm(N, mean, sd) ,
Where N is for how many such random numbers to generate, mean and sd for the mean and standard deviation of the distribution and default values are 0 and 1 for these two parameters. Next code block show several examples of generating random numbers from specified Normal distributions.
#generate 10 random numbers from standard normal distribution
#standard normal distribution (mean=0, sd=1)
vec_norm1 <- rnorm(10)
vec_norm1
#output
[1] -0.10300454 -0.49992423 -0.04867705 -0.20479786 0.64047272
[6] 0.80908181 2.63997308 0.54729061 -1.53948859 -0.56861347
#10 random numbers from normal variates with mean 32
#and standard deviation 2
vec_norm2 <- rnorm(10, mean=32, sd=2)
vec_norm2
#output
[1] 31.77717 29.00409 33.75556 32.12455 29.60058 33.79212 32.04183
[8] 33.66872 31.60234 31.13245
# a matrix of 50 normal variates with mean 32 sd 2
mat_norm <- matrix(rnorm(50,mean=32, sd=2),nrow=10)
mat_norm
#output
[,1] [,2] [,3] [,4] [,5]
[1,] 32.17958 27.05001 36.68007 30.66535 33.00489
[2,] 31.97359 33.11925 32.96932 30.13305 33.49547
[3,] 32.30013 32.74311 33.43902 32.04489 32.22167
[4,] 35.18345 28.34490 32.31010 30.84051 29.25815
[5,] 28.83627 34.10811 29.39567 30.01753 34.64026
[6,] 28.89909 27.29548 32.78904 32.68282 32.00039
[7,] 33.32865 31.11026 32.56352 34.01731 34.44393
[8,] 32.18550 35.94802 33.88321 30.73226 32.15024
[9,] 29.06265 30.57460 30.49779 31.30553 30.00966
[10,] 29.73152 30.07022 29.59840 32.81636 35.68608
2. Uniform distribution
Uniform distribution states the situation where a constant probability density exists in a range. The function runif(N, min, max) in R generates Uniform variates, Where
N represents sample size, min and max parameters specify the range. The following code block illustrate examples about Uniform distribution.
#25 uniform variates between 0 and 1
#default range is 0 and 1
vec_uni_1 <- runif(25)
vec_uni_1
#output
[1] 0.96642510 0.38370536 0.99770052 0.99907261 0.38873183
[6] 0.56486749 0.31835318 0.33375417 0.91052761 0.81353888
[11] 0.19505230 0.06403500 0.96022244 0.40504310 0.02559239
[16] 0.66514033 0.96932741 0.55033963 0.52867588 0.13435604
[21] 0.49814712 0.51983895 0.80709462 0.53736817 0.16027540
#10 uniform variates between 3 and 8
vec_uni_2 <- runif(10, min=3, max=8)
vec_uni_2
#output
[1] 5.264467 5.507854 5.859587 5.014579 3.052218 7.628316 3.571432
[8] 4.471559 4.501750 7.780484
3. Poisson distribution
Poisson distribution is modeling event occurrence with a time interval, in which a constant average occurrence rate exists. For example, With a constant event occurring rate 5 telephone calls coming in a call center per hour, the mean of coming calls in a 2 hour period is 10 times. The following code example show the Poisson random numbers (integers) are generated from function rpois(N, mean).
#a matrix of 100 poisson variates with mean 10
matrix(rpois(100, 10),nrow=20)
#output
[,1] [,2] [,3] [,4] [,5]
[1,] 10 9 14 8 8
[2,] 19 14 19 12 11
[3,] 6 10 9 5 7
[4,] 12 9 11 12 12
[5,] 7 15 15 9 6
[6,] 12 14 10 7 3
[7,] 6 14 10 9 13
[8,] 7 7 17 7 15
[9,] 12 7 7 12 8
[10,] 11 10 14 10 12
[11,] 10 5 9 6 11
[12,] 9 7 9 7 4
[13,] 13 5 10 6 10
[14,] 7 8 8 6 11
[15,] 11 9 9 7 9
[16,] 8 9 8 13 10
[17,] 12 5 13 11 7
[18,] 14 11 11 6 14
[19,] 7 12 11 9 12
[20,] 16 14 7 12 15
>
0 Comments