R Code Cheat Sheet
R Code Cheat Sheet
The following is a brief guide describing which functions do what. Code related to Unit 2 and Unit 3
is listed below!
2.2 Code
Binomial
For 𝑿 following a binomial distribution with 𝑛 trials and probability of success 𝑝.
To find 𝑷(𝑿 = 𝒂):
dbinom(a, size = n, prob = p)
Hypergeometric
For 𝑿 following a hypergeometric distribution with 𝑚 successes in the population, 𝑛 failures in the
population, and a sample of 𝑘 observations from the population.
To find 𝑷(𝑿 = 𝒂):
dhyper(a, m, n, k)
Poisson
For 𝑿 following a Poisson distribution with a rate parameter 𝜆.
To find 𝑷(𝑿 = 𝒂):
dpois(a, lambda = lambda)
Normal
For 𝑿 following a normal distribution with mean 𝜇 and standard deviation 𝜎.
To find 𝑷(𝑿 ≤ 𝒂):
pnorm(a, mean = mu, sd = sigma)
3.2 Code
Consider a set of data named dataset with columns named X and Y. Note that in order to use the
column names in any R function, you must attach the data using attach(dataset).
This will open a graphics window that is blank; click in the window to bring up the first of four plots.
The plots will advance with each click in the window. The first plot is your “Residuals vs. Fitted”
plot (used to check homoscedasticity). The second plot is your “Normal Q-Q” plot (used to check
normality).The third (Scale-Location) and fourth (Residuals vs. Leverage) plots will not be
examined in this course.