# 7 Probability Functions

This chapter will primarily focus on the normal distribution functions in R.

## 7.1 Generating Random Numbers

To calculate random numbers in R based on a normal distribution, we can use the `rnorm()` function. By default, the mean and sd respectively are 0 and 1; but we can change these parameters as necessary.

``````set.seed(1) # Remember our random numbers
x <- rnorm(100,       # 100 random numbers
mean = 50, # with a mean of 50
sd = 20)   # and SD of 20.

plot(density(x),
main = '100 Random Numbers')`````` See more on plots in the Graphing chapter.

## 7.2 Sampling

We can take a random sampling of a vector with `sample()`.

``````set.seed(1) # Remember our random values.

# 10 random numbers from
#   a vector of 100 values.
sample(1:100, size = 10, replace = TRUE)``````
``##   68 39  1 34 87 43 14 82 59 51``

For a dataset, we can do the following:

``````set.seed(1) # Remember our random values.

# Random 5 rows
mtcars[sample(1:NROW(mtcars), 5), ]``````
``````##                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Pontiac Firebird 19.2   8  400 175 3.08 3.845 17.05  0  0    3    2
## Hornet 4 Drive   21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Duster 360       14.3   8  360 245 3.21 3.570 15.84  0  0    3    4
## Mazda RX4        21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag    21.0   6  160 110 3.90 2.875 17.02  0  1    4    4``````

## 7.3 Others

See `?rnorm`, `?rchisq`, and `?rpois` for more information on normal, chi-square, and Poisson probability distributions

## 7.4 Summary

Table 7.1: Summary of Probability Distributions
Function Description Example
rnorm(x) x random numbers based on a normal distribution. rnorm(10)
sample(x, size) Sample a vector with a specified size. sample(1:100, size = 10)
Other probability functions. See `?rnorm`, `?rchisq`, and `?rpois`