Sei sulla pagina 1di 3

Turner Math 118

Especially Handy Functions in R

Descriptive Statistics
Mean
mean(X)
Median
median(X)
Mode
Copy the greyed text into R-studio, and press “enter.” Then use the function like
any other function.
#This is a function that finds the mode
mode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}

mode(X)
Standard Deviation
sd(X)
IQR
IQR(X)
Range
#This is a function that finds the range
rangeX <- function(u) {
fr=(max(u)-min(u))
fr
}

rangeX(X)
Five number summary (plus the mean)
summary(X)
Inferential Statistics
The general syntax (and default values) of the t-test is:
t.test(x, y = NULL,
alternative = "two.sided"/"less"/"greater",
mu = 0, paired = FALSE, var.equal = FALSE,
conf.level = 0.95)
One-sample t-test
t.test (X)
Two-sample paired t-test
t.test(X1, X2, paired=TRUE)
Two-sample independent t-test when each sample is in a different vector (column)
t.test(X,Y)
Two-sample independent t-test when the response variable (X) is in one vector (column)
and the grouping variable (Y) is in another vector (column).
t.test(X~Y)
Correlations have the general syntax
cor(x, y, method = "pearson"/"kendall"/"spearman")
Pearson correlation between data in two vectors (columns)
cor(X,Y)
To find a correlation matrix for all columns of a dataset called “my_data”
round(cor(my_data),2)
To find the least squares regression line for the predictor X and response Y.
lm(Y~X)
A scatter plot with regression line can be obtained by
plot(X,Y, xlab=”x-axis label”, ylab=”y-axis label”,
main=”Title”)
abline(lm(Y~X)
The following library offers:
library("ggpubr")
ggscatter(my_data, x = "mpg", y = "wt", add =
"reg.line", conf.int = TRUE, cor.coef = TRUE,
cor.method = "pearson", xlab = "Miles/(US) gallon",
ylab = "Weight (1000 lbs)")
Graphing
Simple pie charts for categorical data:
pie(table(X))
Simple histograms
histogram(X)
More complicated histograms – changing the bin width (n is the number of breaks – i.e.,
one less than the number of bins) and the color comes from the R color space.
histogram(X, main="Title",
xlab="X Axis Label",
col="color",
breaks=n))

Potrebbero piacerti anche