Learn R Programming

DISTRIB (version 1.0)

rd: Compute random data from a distribution

Description

This function creates random data from any usual univariate distribution in R. Unlike the common methods for creating random data from a distribution (such as rnorm, rpois and etc.), the name of the introduced function is fix for any distribution and the name of distribution is considered as an argument in rd function. Therefore creating random data by rd function is applicable for any kind of distribution with an unique function form but by considering the name of $T$ distribution (and its parameters) as two arguments of rd function.

Usage

rd(n, T.dist, T.dist.par)

Arguments

n
The number of random observations which must be created from the considered distribution in argument part.
T.dist
The distribution name of the random variable is determined by characteristic element T.dist. The names of distributions is similar to stats package.
T.dist.par
A vector of distribution parameters with considered ordering in stats package.

Value

This function will create n random data from the considered distribution.

Examples

Run this code
rd(n=10, T.dist="norm", T.dist.par=c(0,1)) # Is equal to  rnorm(10)
rd(25, T.dist="pois", T.dist.par=3.3) # Is equal to  rpois(25,3.3)

## The function is currently defined as
function (n, T.dist, T.dist.par) 
{
    rDis = paste("r", T.dist, sep = "", collapse = "")
    if (length(T.dist.par) == 1) {
        rd.t = do.call(rDis, list(n, T.dist.par[1]))
    }
    else {
        if (length(T.dist.par) == 2) {
            rd.t = do.call(rDis, list(n, T.dist.par[1], T.dist.par[2]))
        }
        else {
            rd.t = do.call(rDis, list(n, T.dist.par[1], T.dist.par[2], 
                T.dist.par[3]))
        }
    }
    return(rd.t)
  }

Run the code above in your browser using DataLab