R has pseudo-random number generators that allow you to simulate the most common probability distributions.
Uniform numbers:
# generate n random numbers between the default values of 0 and 1
runif(n)
# generate n random numbers between 0 and 25
runif(n, min = 0, max = 25)
# generate n random numbers between 0 and 25 (with replacement)
sample(0:25, n, replace = TRUE)
# generate n random numbers between 0 and 25 (without replacement)
sample(0:25, n, replace = FALSE)
Non-uniform probability distribution have four primary functions:
r: random number generation
d: density or probability mass function
p: cumulative distribution
q: quantiles
Normal Distribution Numbers:
# generate n random numbers from a normal distribution with given
# mean & st. dev.
rnorm(n, mean = 0, sd = 1)
# generate CDF probabilities for value(s) in vector q
pnorm(q, mean = 0, sd = 1)
# generate quantile for probabilities in vector p
qnorm(p, mean = 0, sd = 1)
# generate density function probabilites for value(s) in vector x
dnorm(x, mean = 0, sd = 1)
Binomial Distribution Numbers:
rbinom(n, size = 100, prob = 0.5)
# generate CDF probabilities for value(s) in vector q
pbinom(q, size = 100, prob = 0.5)
# generate quantile for probabilities in vector p
qbinom(p, size = 100, prob = 0.5)
# generate density function probabilites for value(s) in vector x
dbinom(x, size = 100, prob = 0.5)
Poisson Distribution Numbers:
rpois(n, lambda = 4)
# generate CDF probabilities for value(s) in vector q when
# lambda (mean rate)
# equals 4.
ppois(q, lambda = 4)
# generate quantile for probabilities in vector p when
# lambda (mean rate)
# equals 4.
qpois(p, lambda = 4)
# generate density function probabilites for value(s) in vector x when
#lambda
# (mean rate) equals 4.
dpois(x, lambda = 4)
Exponential Distribution Numbers:
rexp(n, rate = 1)
# generate CDF probabilities for value(s) in vector q when rate = 4.
pexp(q, rate = 1)
# generate quantile for probabilities in vector p when rate = 4.
qexp(p, rate = 1)
# generate density function probabilites for value(s) in vector x
# when rate = 4.
dexp(x, rate = 1)
Gamma Distribution Numbers:
rgamma(n, shape = 1)
# generate CDF probabilities for value(s) in vector q when
# shape parameter = 1.
pgamma(q, shape = 1)
# generate quantile for probabilities in vector p when
# shape parameter = 1.
qgamma(p, shape = 1)
# generate density function probabilites for value(s) in vector x
# when shape
# parameter = 1.
dgamma(x, shape = 1)
x < y # is x less than y
x > y # is x greater than y
x <= y # is x less than or equal to y
x >= y # is x greater than or equal to y
x == y # is x equal to y
x != y # is x not equal to y
x <- 9
y <- 10
x == y
x <- c(1, 4, 9, 12)
y <- c(4, 4, 9, 13)
x == y
# How many pairwise equal values are in vectors x and y
sum(x == y)
# Where are the pairwise equal values located in vectors x and y
which(x == y)
Exact Equality:
x <- c(4, 4, 9, 12)
y <- c(4, 4, 9, 13)
identical(x, y)
x <- c(4, 4, 9, 12)
y <- c(4, 4, 9, 12)
identical(x, y)
Floating Point Comparison:
x <- c(4.00000005, 4.00000008)
y <- c(4.00000002, 4.00000006)
all.equal(x, y)
# If the difference is greater than the tolerance level
# the function will return the mean relative difference:
x <- c(4.005, 4.0008)
y <- c(4.002, 4.0006)
all.equal(x, y)
## [1] "Mean relative difference: 0.0003997102"
#Rounding numeric Values
x <- (1, 1.35, 1.7, 2.05, 2.4)
# Round to the nearest integer
round(x)
## [1] 1 1 2 2 2
# Round up
ceiling(x)
## [1] 1 2 2 3 3
# Round down
floor(x)
## [1] 1 1 1 2 2
# Round to a specified decimal
round(x, digits = 1)
## [1] 1.0 1.4 1.7 2.0 2.4