The Wharton School | Groups

Loading
Loading

 
Handy R functions for all of your STAT 101 needs.
 

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.812
  • median(vec) is 5.5
  • min(vec) is 0
  • max(vec) is 9
All of them at once, plus the quartiles
  • summary(vec)
Interquartile Range - Nice for outlier problems
  • IQR(vec) is 5
Standard Deviation and Variance
  • var(vec) is 8.69
  • sd(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)