Functions

Built-in function with R

Just like most packages, R has many built-in functions. Below is the list of some, x being a vector or a matrix.

Arithmetic operations

 Operation Description
 +  Addition
 -  Substraction
 *  Multiplication
 /  Division
 ** or ^   Exponentiation

Numerical Functions (The list is by ne means exhaustive!)

 Function  Description
 abs(x)  absolute value
 sqrt(x)   square root
 ceiling(x)   ceiling(3.475)  is 4
 floor(x)   floor(3.475)     is 3
 trunc(x)   trunc(5.99)      is 5
 round(x, digits=n)   round(3.475, digits=2) is 3.48
 signif(x, digits=n)   signif(3.475, digits=2) is 3.5
 cos(x), sin(x), tan(x)  acos(x), cosh(x), acosh(x),
etc..
 cosine(x), sine(x), arcosine(x) hyperbolic cosine(x), hyperbolic arcosine(x),etc..
 log(x)  natural logarithm ln(x)
 log10(x)  logarithm base 10  of x
 exp(x)   exponential of x
 
Statistical Functions (The list is by ne means exhaustive!)
 
 Function  Description
 mean(x, trim=r , na.rm=FALSE)  r % trimmed mean of object x
 sd(x)  standard deviation of object x
 var(x)  variance of object x
 mad(x)   median absolute deviation of object x
 median(x)   median of object x
 range(x)  range of object x
 sum(x)   sum of the values of objet x
 min(x)   minimum value of object x
 max(x)   maximum values of object x

Statistical Distribution Functions
 
Given a random variable X, taking values x, one is usually interested in four quantities:
  1. f(x), where f() is  the p.d.f (probability density function) or the p.m.f of X, 
  2. F(x) ,where F() is the  c.d.f (cumulative probability distribution) of X,
  3. quantile(p) , where quantile() is the p-th quantile of the distribution of X,
  4. random(n), where random() is a random generator of n points from the distribution of X.
R already has some distribution encoded and the syntax is as follows. Suppose the distribution of X is mydistribution
then
 
 Function     Description
 dmydistribition(x)  density function f(x)
 pmydistribution(x)  cumulative distribution F(x)
 qmydistribution(p)  pth-quantile rfom the distribution of X
 rmydistribution(n)  n random numbers from the distribution of X

Some distribution names include

 
mydistribution  Description
 unif  Uniform distribution
 norm  Normal distribution
 exp      Exponential distribution
 t  Student-t distribution
 gamma  Gamma distribution
 chisq  Chi-squared distribution
 lnorm      Log-normal distribution
 weibill  Weibull distribution
 cauchy  Cauchy distribution
 binom  Binomial distribution
 pois      Poisson distribution
 geom  Geometric distribution
hyper  Hypergeometric distribution

Example of use:
 
Suppose X is normally distributed with mean 2 and standard deviation 1, and we want: (a) P(X<1.65)  (b) Find x such that P(X>x)=0.05. (c) Generate 5 random point from the distribution of X.
We would do sequentially the following:
 
 

>pnorm(1.65,2,1,lower.tail=TRUE)       # Question (a)
[1] 0.3631693
>qnorm(0.05,2,1,lower.tail=FALSE)      # Question (b)
[1] 3.644854
>rnorm(5,2,1)                          # Question (c)
[1] 0.7261126 3.1286553 1.0042537 2.8120058 3.9837978

 
Note that as for the last command, you may not obtain the same result as above because of randomness!