sample(x = 1:10, size = 4, replace = F)
[1] 7 4 10 2
(AST230) R for Data Science
The function sample()
is used to generate random values from a vector, and it has the following arguments:
x
\rightarrow A vector of outcome you want to sample fromsize
\rightarrow The number of samples (observations) you want to drawreplace
\rightarrow It can take either TRUE
or FALSE
prob
\rightarrow Specifies probability of selection of different elements of x
Select 10 numbers from 0 to 100
R has built in many functions for conveniently working with a large number of distributions.
The quantities that are of main interest from any probability distribution are:
Distribution | Density Function | Cumulative Distribution | Quantile | Random Variates |
---|---|---|---|---|
Normal | dnorm() |
pnorm() |
qnorm() |
rnorm() |
Poisson | dpois() |
ppois() |
qpois() |
rpois() |
Binomial | dbinom() |
pbinom() |
qbinom() |
rbinom() |
Uniform | dunif() |
punif() |
qunif() |
runif() |
rbinom()
and rnorm
rbinom()
is used to draw a sample from a binomial distribution
size
\rightarrow number of Bernoulli trials
prob
\rightarrow probability of success
n
\rightarrow number of observations
Draw a sample of size 8 from B(10, 0.75)
rnorm()
is used to draw a sample from a normal distribution
mean
\rightarrow mean of the distribution (\mu)
sd
\rightarrow standard deviation of the distribution (\sigma)
n
\rightarrow number of observations
Draw a sample of size 5 from N(10, 16)
pnorm()
For X \sim N(50, 3^2), find P(45<X<55).
P(a < X ≤ b) = F(b) − F(a)
dnorm()
qnorm()