dnorm() function in R allows you to calculate the probability density function for a normal distribution for a given mean and standard deviation. This guide shows you how to use the dnorm() function in R.
dnorm() syntax
dnorm(x, mean = 0, sd = 1)
Arguments
- x = numeric value to test for probability
- mean = mean of the distribution
- sd = standard deviation of the distribution
The mean and sd arguments are optional. If you do not pass a value to them, they will take the default values as mean = 0 and sd = 1.
Example 1: dnorm() function with a single value
Suppose we want to calculate the probability density function of 5 with a mean of 10 and a standard deviation of 3.
x <- dnorm(5, mean=10, sd=3)
Output
[1] 0.03315905
Example 2: dnorm() Function with a vector of values
Say we want to calculate the probability density function of each element inside a vector with a mean of 10 and a standard deviation of 3.
#creating a vector
my_data <-c(1,2,3,4,5)
#calculating probability density function of the vector
x <- dnorm(my_data, mean=10, sd=3)
x
Output
[1] 0.001477283 0.003798662 0.008740630 0.017996989 0.033159046