Statistical distributions are the meat and potatoes of R. Generating random numbers from any distribution is easy in R. Below I have listed the code for several popular statistical distributions. The code is nearly the same for each distribution. The “r” designates random, which is the first letter of the call for the distribution. “unif” calls a uniform distribution, “norm” calls a normal distribution, “binom” calls a binomial distribution, etc.

To get random data from the normal distribution (rnorm), several other parameters are needed. n is the number of random variables to create, mean is the mean of the data distribution, and sd is the standard deviation of the data distribution. I am requesting 10 random variables from the normal distribution with a mean of 0 and a standard deviation of 1.

#Uniform Distribution
runif(n=10, min=0, max=1)
#Normal Distribution
rnorm(n=10, mean=0, sd=1)
#Binomial Distribution
rbinom(n=10, size=5, prob=0.2)
#The log-normal Distribution
rlnorm(n=10, meanlog=0, sdlog=1)
#Weibull Distribution
rweibull(n=10, shape=1, scale = 1)
#Exponential Distribution
rexp(n=10, rate = 1)
#Poisson Distribution
rpois(n=10, lambda=1)
#Gamma Distribution
rgamma(n=10, shape=1, rate = 1)
#Chisquare Distribution
rchisq(n=10, df=3, ncp=1)
#where df is degrees of freedom, and ncp is non-centrality parameter

### Like this:

Like Loading...

*Related*