Handy R functions for all of your STAT 101 needs.
Mean, Median, Min, and Max
Mode of a Sample - Not included by default, works for both numeric & categorical
Descriptive Statistics for Numerical Data
Mean, Median, Min, and Max
vec <- c(9, 2, 6, 7, 2, 1, 5, 6, 8, 9, 1, 0, 3, 5, 6, 7)
mean(vec)
is 4.812median(vec)
is 5.5min(vec)
is 0max(vec)
is 9
summary(vec)
IQR(vec)
is 5
var(vec)
is 8.69sd(vec)
is 2.94
Mode of a Sample - Not included by default, works for both numeric & categorical
getModes <- function(x) {
ux <- unique(x)
tab <- tabulate(match(x, ux))
ux[tab == max(tab)]
}
getModes(vec)
getModes(c(5, 5, 3, 3, 1, 7))
is a vector with 5 and 3
Descriptive Statistics for Categorical Data
df <- datasets::chickwts
table(df$feed)