# What is dnorm() Function in R

The dnorm() function gives the density. The pnorm() function gives the distribution function. The qnorm() function gives the quantile function. The rnorm() function generates random deviates.

## dnorm in R

The dnorm() in R is a built-in function that calculates the density function with a mean( and standard deviation( for any value of x. The dnorm() function takes a vector, mean, sd, and log as arguments and returns the Probability Density Function.

For a discrete distribution (like the binomial), use the dnorm() function to calculate the density (p. f.), which in this case is probability.

### Syntax

dnorm(x, mean = 0, sd = 1, log = FALSE)

### Parameters

x: vector of quantiles.

mean: vector of means.

sd: vector of standard deviation.

log, log.p: logical; if TRUE, probabilities p are given as log(p).

If the mean and sd are not specified, they assume the default values of 0 and 1, respectively.

### Example

dnorm(0, mean = 0, sd = 1)

#### Output

dt <- dnorm(0, mean = 0, sd = 1)
dt

#### Output

[1] 0.3989423

The dnorm of 0 is the following.

dt <- dnorm(0)
dt

#### Output

[1] 0.3989423

Let’s find the value of the normal distribution pdf at x=10 with mean=20 and sd=5.

dt <- dnorm(x=10, mean=20, sd=5)
dt

#### Output

[1] 0.01079819

Let’s find the dnorm() of 1:3.

dnorm(1:3)

#### Output

[1] 0.241970725 0.053990967 0.004431848

## Creating a plot based on the dnorm() function

The most powerful application of the dnorm() function is that it is in creating a normal distribution plot in R.

Let’s create a sequence using the seq() function.

data <- seq(-3, 3, length = 30)
dt <- dnorm(data)
dt

#### Output

 [1] 0.004431848 0.008069595 0.014077583 0.023529569 0.037679866 0.057811501
[7] 0.084982332 0.119688537 0.161505004 0.208799242 0.258631467 0.306932813
[13] 0.348991450 0.380185689 0.396813332 0.396813332 0.380185689 0.348991450
[19] 0.306932813 0.258631467 0.208799242 0.161505004 0.119688537 0.084982332
[25] 0.057811501 0.037679866 0.023529569 0.014077583 0.008069595 0.004431848

Now, let’s create a plot based on these values.

data <- seq(-3, 3, length = 30)
dt <- dnorm(data)
plot(data, dt, type = "l", lwd = 1.5, axes = FALSE, xlab = "", ylab = "")
axis(1, at = -3:3, labels = c("-3s", "-2s", "-1s", "mean", "1s", "2s", "3s"))

## Find the Area Under the Normal Density Curve

The total area under any normal density curve is always equal to one. Let’s see an example of that.

x <- seq(-3, 3, length = 200)
y <- dnorm(x, mean = 0, sd = 1)
plot(x, y, type = "l")
x <- seq(-3, 0, length = 100)
y <- dnorm(x, mean = 0, sd = 1)
polygon(c(-3, x, 0), c(0, y, 0), col = "blue")

#### Output

Now, because the total area under the curve is 1, and because of the symmetry, the area to the left of μ=0 should be 0.5.

That is it for the dnorm() function in R.