Summary statistics

mean, median, sd, quantile, table() for counts, correlation, summary(), and reproducible random numbers with set.seed().

Runs in Zerethon IDE · R 4.5 Official R documentation ↗
main.R
scores <- c(70, 95, 82, 60, 88, 82)
print(unname(quantile(scores)))
cat(sprintf("mean %.2f, median %g, sd %.2f\n", mean(scores), median(scores), sd(scores)))
counts <- table(c("a", "b", "a", "c", "a"))
cat(sprintf("%s=%d", names(counts), counts), sep = ", ")
cat("\n")
print(unname(quantile(scores, 0.9)))
print(round(cor(1:5, c(2, 4, 6, 8, 11)), 3))
set.seed(42)
print(round(runif(3), 2))
print(sample(1:10, 3))

Output

[1] 60.0 73.0 82.0 86.5 95.0
mean 79.50, median 82, sd 12.61
a=3, b=1, c=1
[1] 91.5
[1] 0.996
[1] 0.91 0.94 0.29
[1] 9 4 2
  • mean, sd and the others return NA if the data has an NA; pass na.rm = TRUE to skip it.
  • quantile(x) gives the minimum, quartiles and maximum; summary(x) prints the same with the mean, labelled, and summary(df) describes every column of a data frame.
  • table() counts how often each value occurs; prop.table(table(x)) gives the shares.
  • set.seed(n) makes random numbers repeat exactly, run after run.
  • The recommended packages that ship with R (MASS, lattice, Matrix, survival, …) can be loaded with library().

Tip: A run has no screen for plots: plot() tries to write Rplots.pdf into the read-only project and stops with cannot open file 'Rplots.pdf'. Print numbers and tables instead.

Related topics

Official references