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:
Syntax
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
scores <- c(85, 90, 78, 92, 88)
cat("Mean:", mean(scores), "\n")
cat("Median:", median(scores), "\n")
cat("Std Dev:", sd(scores), "\n")
Login to try C/C++/Java/PHP code in the editor
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
- Calling
mean(scores)on a vector withNAand gettingNA, when you needna.rm = TRUE. - Confusing
sd()with the population standard deviation, when it usesn - 1, the sample formula. - Passing several numbers as separate arguments, such as
mean(1, 2, 3), which only uses the first; usemean(c(1, 2, 3)).
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: