R Commands
R Commands
R Commands
Storing Data:
x <- c(100,200,300,400) c stands for concatenate
x = c(100,200,300,400) same as above
x = 1:100 makes the list x = c(1,2,3,...,100)
Miscellany:
sort(x) sorts list of data x
length(x) gives the number of points in dataset (i.e., the sample size) of x
sum(x) sums the values in the list of data x
x*y Given two lists of data, x and y, each the same length, it produces a
new list of data consisting of the products of the two lists.
table(x) Makes a frequency table for the dataset x
stem(x) Creates a stem-and-leaf plot for x
format(x,scientific = F) Prints the number x in decimal format
Using R as a calculator:
+-*/ 4 basic arithmetic operations
2**3 or 2^3 gives 2 to the third power (exponentiation)
sqrt(9) gives square root of 9
abs(-3) gives absolute value of -3
factorial(4) gives 4! = (4)(3)(2)(1) = 24
choose(10,3) gives C(10,3) = 10!/(7!3!) = 120
To produce P(10,3), use that P(10,3) = C(10,3)*factorial(3)
Measures of Spread:
sd(x) sample standard deviation of x
var(x) sample variance of x
range(x) Outputs max and min of data set
fivenum(x) produces 5-number summary
Boxplots have too many quirks to be useful for this course**
Discrete Random Variables:
sum(x*p) Computes expected value of a discrete random variable whose
outputs are x and corresponding probabilities p
sqrt(sum((x -m)^2*p)) Computes the standard deviation of a random variable whose
outputs are x and corresponding probabilities p
Student’s t-Distribution
dt(x,df) Gives y-value (height) of the t-distribution at x with df degrees of freedom.
pt(x,df) Gives the cumulative probability at x in a t-distribution with df degs of freedom.
qt(α,df) Gives the x-value such that pt(x,df) = α.
qt(α,df,lower.tail=FALSE) Returns the value 𝑡
α
rt(n, df)Gives n random value from a t-distribution with df degrees of freedom.
Ch. 9: One Sample Student’s t-test (determining one population mean)
t.test(x, y = NULL, alternative = c("two.sided", "less", "greater"), mu=mu_0, conf.level = 1-α)
Performs the one-sample t-test. The variable x is the data, mu = mu_0 (H_0), alternative
gives the type of alternative hypothesis, and conf.level gives the confidence level (1-α).
This test will automatically compute p-values, but it is only valid for the t-test (and not the
z-test). The default value of alternative is "two.sided", the default of mu is 0, and the default
of conf.level is 0.95 (i.e. α = 0.05).
Ch. 9: P-Values
1. If the population standard deviation is known and z is your test statistic:
a. Two-sided: p = 2*pnorm(-abs(z))
b. Left-Tailed: p = pnorm(-abs(z))
c. Right-Tailed: p = pnorm(-abs(z))
2. If the population st. deviation is unknown and t is your test statistics (with df deg of freedom):
a. Two-side: p = 2*pt(-abs(t),df)
b. Left-Tailed: p = pt(-abs(t),df)
c. Right-Tailed: p = pt(-abs(t),df)