We provide effective and economically affordable online training courses for R and Python, click here for more details and course registration !

Normal distribution is a continuous random variable distribution with bell-shaped probability density curve. It is widely used in statistical data analysis, and the basis for many other distributions as well. The probability density function can be expressed as

Normal distribution probability density function

In which the two parameters μ and σ, are its mean and standard deviation, respectively.

The probability under the curve between any two x values x = x1 and x = x2 equals the integral

Probability under Normal probability density curve

With R programming, there are generally following functions that are used to calculate for Normal distribution.

dnorm() – probability density

pnorm() – cumulative probability up to a specified value

qnorm() – quantile value, below which is the specified cumulative probability, and this is the opposite operation of pnorm()

rnorm() – to generate random number from a determined normal distribution

In the next code examples, you can see how to implement normal distributions with these functions.

# creating a sequence of values 
# between -3 to 3 with a step of 0.1
T = seq(-3, 3, by=0.1)
T
#output
[1] -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8
[14] -1.7 -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5
[27] -0.4 -0.3 -0.2 -0.1  0.0  0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8
[40]  0.9  1.0  1.1  1.2  1.3  1.4  1.5  1.6  1.7  1.8  1.9  2.0  2.1
[53]  2.2  2.3  2.4  2.5  2.6  2.7  2.8  2.9  3.0
#calculate probability density for these values
#from a normal distribution with mean 0.5 standard deviation 1.2
dt = dnorm(T, mean=0.5, sd=1.2)
dt
#output
[1] 0.004725734 0.006005083 0.007577969 0.009496655 0.011818778
 [6] 0.014606917 0.017927867 0.021851574 0.026449710 0.031793853
[11] 0.037953294 0.044992472 0.052968089 0.061925970 0.071897766
[16] 0.082897616 0.094918912 0.107931330 0.121878295 0.136675062
[21] 0.152207571 0.168332238 0.184876796 0.201642270 0.218406128
[26] 0.234926563 0.250947860 0.266206671 0.280439019 0.293387772
[31] 0.304810305 0.314486023 0.322223431 0.327866430 0.331299555
[36] 0.332451900 0.331299555 0.327866430 0.322223431 0.314486023
[41] 0.304810305 0.293387772 0.280439019 0.266206671 0.250947860
[46] 0.234926563 0.218406128 0.201642270 0.184876796 0.168332238
[51] 0.152207571 0.136675062 0.121878295 0.107931330 0.094918912
[56] 0.082897616 0.071897766 0.061925970 0.052968089 0.044992472
[61] 0.037953294
# Plot the graph.
plot(T, dt)
Normal distribution probability density output from RStudio
#Cumulative probabilities for these values
#from a normal distribution with mean 0.5 standard deviation 1.2
PT <- pnorm(T, mean = 0.5, sd = 1.2)
PT
#output
[1] 0.001768968 0.002303266 0.002979763 0.003830381 0.004892537
 [6] 0.006209665 0.007831677 0.009815329 0.012224473 0.015130140
[11] 0.018610425 0.022750132 0.027640146 0.033376508 0.040059157
[16] 0.047790352 0.056672755 0.066807201 0.078290204 0.091211220
[21] 0.105649774 0.121672505 0.139330247 0.158655254 0.179658669
[26] 0.202328381 0.226627352 0.252492538 0.279834464 0.308537539
[31] 0.338461120 0.369441340 0.401293674 0.433816167 0.466793248
[36] 0.500000000 0.533206752 0.566183833 0.598706326 0.630558660
[41] 0.661538880 0.691462461 0.720165536 0.747507462 0.773372648
[46] 0.797671619 0.820341331 0.841344746 0.860669753 0.878327495
[51] 0.894350226 0.908788780 0.921709796 0.933192799 0.943327245
[56] 0.952209648 0.959940843 0.966623492 0.972359854 0.977249868
[61] 0.981389575
# Plot the graph for cumulative probabilities
plot(T, PT)
Normal distribution cumulative probabilities output from RStudio
# Create a sequence of values representing cumulative probabilities 
prob_vec <- seq(0, 1, by = 0.05)
prob_vec
#output
[1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60
[14] 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00
#quantile values associated with these cumulative probabilities
#from a normal distribution with mean 0.5 standard deviation 1.2
QT <- qnorm(prob_vec, mean=0.5, sd=1.2)
QT
[1]        -Inf -1.47382435 -1.03786188 -0.74372007 -0.50994548
 [6] -0.30938770 -0.12928062  0.03761544  0.19598348  0.34920638
[11]  0.50000000  0.65079362  0.80401652  0.96238456  1.12928062
[16]  1.30938770  1.50994548  1.74372007  2.03786188  2.47382435
[21]         Inf
# Plot the graph for quantile values
plot(prob_vec, QT)
Quantile values from a normal distribution, RStudio output
# Randomly generate 10000 numbers from a normal distribution
# with mean=0.5 and standard deviation=1.2
RT <- rnorm(10000, mean=0.5, sd=1.2)
# Plot generated random numbers in a histogram, with 50 bins
hist(RT, breaks=50)
Hisrogram for generated random numbers from a normal distribution

For getting more knowledge of R and a preview of our training course, you can watch R tutorial videos on our YouTube channel !

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago