← Back to R Course | Chapter 9: Statistics | Lesson 1 of 7

mean, median, sd

Mean, median, and sd describe a group of numbers: the average, the middle one, and how spread out they are. It is like summarizing class test scores with a few facts.

In this page:

  1. mean, median, sd
Syntax
r
mean(x)
median(x)
sd(x)
mean(x, na.rm = TRUE)

mean, median, sd

mean() computes the arithmetic average, median() finds the middle value, and sd() computes the standard deviation of a numeric vector.

All three are fundamental descriptive statistics used to summarize a dataset's central tendency and spread. Each has an na.rm = TRUE argument to ignore missing values when computing the result.

Note: If a vector contains NA, mean() returns NA by default -- pass na.rm = TRUE to ignore missing values.

Example: mean, median, sd

markup
scores <- c(85, 90, 78, 92, 88)

cat("Mean:", mean(scores), "\n")
cat("Median:", median(scores), "\n")
cat("Std Dev:", sd(scores), "\n")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling mean(scores) on a vector with NA and getting NA, when you need na.rm = TRUE.
  2. Confusing sd() with the population standard deviation, when it uses n - 1, the sample formula.
  3. Passing several numbers as separate arguments, such as mean(1, 2, 3), which only uses the first; use mean(c(1, 2, 3)).
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.