How to Perform Descriptive Statistics in R (With Examples)

How to Perform Descriptive Statistics in R

Descriptive statistics play a crucial role in understanding and summarizing the main features of a dataset. They provide a concise summary of the essential characteristics of the data, such as central tendency, dispersion, and shape of the distribution.

Common measures of descriptive statistics include mean, median, mode, standard deviation, range, and quartiles.

Descriptive Statistics in R

1. mean()

The mean() function calculates the arithmetic mean of a numeric vector.

# Calculate the mean of a numeric vector
data <- c(10, 15, 20, 25, 30)
mean_value <- mean(data)
print("Mean:")
print(mean_value)
[1] "Mean:"
> print(mean_value)
[1] 20

Check out our online statistics calculators for FREE

2. median()

The median() function calculates the median (middle value) of a numeric vector.

# Calculate the median of a numeric vector
median_value <- median(data)
print("Median:")
print(median_value)
[1] "Median:"
> print(median_value)
[1] 20

3. sd() – Standard Deviation

The sd() function calculates the standard deviation, which measures the amount of variation or dispersion in a set of values.

# Calculate the standard deviation of a numeric vector
standard_deviation <- sd(data)
print("Standard Deviation:")
print(standard_deviation)
[1] "Standard Deviation:"
> print(standard_deviation)
[1] 7.905694

4. summary()

The summary() function provides a summary of the statistical properties of a dataset, including the minimum, maximum, median, and quartiles.

# Generate a summary of a dataset
summary_data <- summary(data)
print("Summary:")
print(summary_data)
[1] "Summary:"
> print(summary_data)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
     10      15      20      20      25    

5. quantile()

The quantile() function calculates the quartiles of a dataset.

# Calculate quartiles
quartiles <- quantile(data)
print("Quartiles:")
print(quartiles)
[1] "Quartiles:"
> print(quartiles)
  0%  25%  50%  75% 100% 
  10   15   20   25   30