R Mean Median and Mode

📊 R Mean, Median, and Mode

In R, Mean, Median, and Mode are measures of central tendency.

They help you understand the center or typical value of a dataset.


 Sample Data



 1. Mean (Average)

The mean is the sum of all values divided by the number of values.

mean(x)

Formula:

Mean=Sum of valuesNumber of values\text{Mean} = \frac{\text{Sum of values}}{\text{Number of values}}

Mean with missing values (NA)



 2. Median (Middle Value)

The median is the middle value when data is sorted.

median(x)

📌

  • If count is odd → middle value

  • If count is even → average of two middle values


 3. Mode (Most Frequent Value)

R does not have a built-in mode function, but we can create one.

Custom Mode Function


 

✔ Returns the most frequent value


 4. Mean, Median, Mode Together


 5. Mean & Median in Data Frames


 


 6. When to Use What?

Measure Best Used When
Mean Data is evenly distributed
Median Data has outliers
Mode Categorical or repeated values

 7. Effect of Outliers


 

📌 Mean affected by outliers, median is not.


📌 Summary

  • Mean → average value

  • Median → middle value

  • Mode → most frequent value

  • Use na.rm = TRUE to ignore missing data

  • Median is best when data has outliers

You may also like...