Descriptive Statistics Functions in R (mean, median, sd, var)
In the fields of data science and statistics, understanding measures of central tendency and dispersion is the first step in any analysis. R serves as a powerful tool for statistical analysis, offering a rich suite of efficient functions to perform descriptive statistics. Among these, mean, median, sd, and var are four fundamental yet indispensable functions used respectively to calculate the mean, median, standard deviation, and variance. This article delves into their core concepts, syntax structures, and application scenarios to help beginners get started quickly.
The mean is calculated by summing all numerical values in a dataset and dividing by the count of those values; it represents the average level of the data. In R, the mean() function computes the arithmetic average of vectors. This function applies not only to numeric vectors but also automatically ignores missing values (NA), a feature that makes it highly practical for real-world data analysis.
# Create an example dataset containing some missing values
data <- c(10, 20, 30, NA, 50)
# Calculate the mean, skipping NA values automatically
mean_value <- mean(data)
print(mean_value)
# Output result: 30
It is worth noting that mean() features another important parameter named na.rm (remove missing values). When set to TRUE, it explicitly removes missing values before calculation, which proves very useful when dealing with mixed-type vectors or requiring more flexible control. The mean is highly sensitive to outliers; therefore, in cases where the data distribution is severely skewed, it may fail to accurately reflect the center of the dataset.
Advantages of the Median: median()
When extreme values (outliers) exist within a dataset or when the distribution appears noticeably skewed, the mean can become misleading. Under such circumstances, the median becomes a more robust choice. The median is defined as the value located in the middle position after arranging a set of data from smallest to largest; it remains unaffected by extreme values. R's median() function is specifically designed for calculating medians.
# Create skewed distribution data containing an outlier
skewed_data <- c(1, 2, 3, 4, 5, 100)
# Calculate the median
median_value <- median(skewed_data)
print(median_value)
# Output result: 3
As shown in this example, despite the presence of a significantly large value (100), the median remains stable at 3, whereas the mean is substantially inflated by it. In practical applications, if the data distribution is unknown or outliers are suspected to exist, prioritizing median() for descriptive statistics often provides more authentic information regarding central tendency.
Measuring Dispersion: sd() and var()
Data dispersion reflects the degree to which individual data points deviate from the center; it serves as a key indicator in assessing variability within a dataset. R offers two closely related functions for this purpose: var() calculates Variance, while summary() or standalone use of sd() computes the Standard Deviation.
Variance (var) is defined as the average of the squared differences between each data point and the mean. In R, var(x) defaults to calculating sample variance with a denominator of $n-1$ (degrees of freedom), adhering to statistical principles for unbiased estimation.
# Calculate variance
data <- c(5, 6, 7, 8, 9)
variance <- var(data)
print(variance)
# Output result: 2
Standard Deviation (sd) is the arithmetic square root of the variance; its unit matches that of the original data, making it more intuitive for interpretation. The sd(x) function similarly defaults to calculating sample standard deviation.
# Calculate standard deviation
std_dev <- sd(data)
print(std_dev)
# Output result: 1.414214
Variance and standard deviation together form the core toolkit for describing data variability. Through these two metrics, we can quantify stability: a smaller variance indicates that data points are concentrated closer to the mean, whereas a larger one suggests a more dispersed distribution. In practical reports, using the standard deviation is generally preferred to intuitively display the range of dispersion within the data.
Comprehensive Practice and Function Selection Guidelines
In actual projects, we often need to examine multiple descriptive statistics simultaneously for a comprehensive understanding of a dataset. While R's built-in summary() function can output minimums, maximums, medians, and means (for numeric variables) in one go, obtaining the standard deviation or variance individually still requires using specific functions as described above. Furthermore, when calculating population-level statistics, setting the parameter na.rm to TRUE allows for handling missing data within the dataset, ensuring result accuracy.
In summary, mean(), median(), sd(), and var() constitute the foundation of descriptive statistics in R. The mean and median assist us in locating the center of the data, while variance and standard deviation reveal its range of variability. Mastering these four functions along with their parameter characteristics will lay a solid groundwork for subsequent complex inferential statistical analysis and data visualization tasks.