You are on page 1of 1

#######################################

# R Quick Reference #
#######################################

############# Vectors ###############

> X = c(1, 2, 3, 4, 5) // creates a string of numbers


// X is now a vector that contains numbers 1-5
// Note: “=“ can be replaced with “<-“

>X // prints the vector


[1] 1 2 3 4 5 // R sample output

############# Graphing ##############

> plot(X, Y) // creates a dot plot vector X and vector Y


// first variable is plotted along horizontal axis
// second variable is plotted along vertical axis

> boxplot(X) // creates a box plot of vector X

############# Distributions ###########

> help(Distributions) // opens full list of distributions available in R

Each distribution has four commands. Each command is prepended with


a letter to indicate functionality.
Prefixes:
“d” // returns height of pdf
“p” // returns cdf
“q” // returns inverse cdf (quantiles)
“r” // returns randomly generated numbers

Normal Distribution
> help(Normal)
Commands:
dnorm // returns height of pdf at given value/s
// P(X = x)

Usage:
> X = c(1, 2, 3, 4, 5)
> dnorm(X) // mean = 0, sd = 1 by default
[1] 2.4197e-01 5.3990e-02 4.4318e-03 1.3383e-04 1.4867e-06
> dnorm(10, 5, 3) // x = 10, mean = 5, sd = 3
[1] 0.03315905

pnorm // returns cdf


// P(X <= x) = P(X < x)

Usage:
> X = c(1, 2, 3, 4, 5)
> pnorm(X) // mean = 0, sd = 1, by default
[1] 0.8413447 0.9772499 0.9986501 0.9999683 0.9999997
> pnorm(10, 5, 3) // mean = 5, sd = 3
[1] 0.9522096

qnorm // inverse of pnorm


// returns number whose cdf matches given probability
// this is the Z-score of given probability

Usage:
> qnorm(0.5) // mean = 0, sd = 1, by default
[1] 0 // Z-score
> qnorm(0.9, 5, 3) // mean = 5, sd = 3
[1] 8.844655 // Z-score

t Distribution
> help(TDist)
commands:
dt
pt
qt
rt

Binomial Distribution
> help(Binomial)
commands:
dbinom
pbinom
qbinom
rbinom

You might also like