To calculate descriptive statistics in R, you can use summary() function or describeBy() function from psych package.

The following methods show how you can do it with syntax.

Method 1: Use summary() Function

summary(df)


Method 2: Use describeBy() Function

library(psych)

describeBy(df, group)


The following examples shows how to calculate descriptive statistics in R.

## Use summary() Function to Calculate Descriptive Statistics

Let see how we can use summary() function to calculate statistical values of data frame:

# Create data frame
df <- data.frame(A=c(45.12,52.12,62.23,45.23,52.10),
B=c(78.10,78.36,78.96,89.52,98.36),
C=c(100.12,101.23,102.25,103.85,104.25))

# Get statistical values
stat<-summary(df)

# Display statistical values
print(stat)


Output:

 A               B               C
Min.   :45.12   Min.   :78.10   Min.   :100.1
1st Qu.:45.23   1st Qu.:78.36   1st Qu.:101.2
Median :52.10   Median :78.96   Median :102.2
Mean   :51.36   Mean   :84.66   Mean   :102.3
3rd Qu.:52.12   3rd Qu.:89.52   3rd Qu.:103.8
Max.   :62.23   Max.   :98.36   Max.   :104.2


As the output shows statistical values for entire data frame which calculated using summary() function.

## Use describeBy() Function to Calculate Descriptive Statistics

Let’s see how to use describeBy() function to calculate descriptive statistics of data frame:

# Import library
library(psych)

# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","A","B","C","D"),
Pressure=c(78.2, 78.2, 71.7, 80.21, 80.21, 82.56, 72.12, 73.85),
Temperature=c(35, 36, 36, 38, 32, 32, 31, 34))

# Calculate descriptive statistics
d <- describeBy(df, df\$Machine_name)

# Show descriptive statistics
print(d)


Output:

 Descriptive statistics by group
group: A
vars n mean   sd median trimmed  mad  min   max range skew kurtosis  se
Machine_name*    1 2  1.0 0.00    1.0     1.0 0.00  1.0  1.00  0.00  NaN      NaN 0.0
Pressure         2 2 79.2 1.42   79.2    79.2 1.49 78.2 80.21  2.01    0    -2.75 1.0
Temperature      3 2 33.5 2.12   33.5    33.5 2.22 32.0 35.00  3.00    0    -2.75 1.5
-----------------------------------------------------------------------
group: B
vars n  mean   sd median trimmed  mad  min   max range skew kurtosis   se
Machine_name*    1 2  2.00 0.00   2.00    2.00 0.00  2.0  2.00  0.00  NaN      NaN 0.00
Pressure         2 2 80.38 3.08  80.38   80.38 3.23 78.2 82.56  4.36    0    -2.75 2.18
Temperature      3 2 34.00 2.83  34.00   34.00 2.97 32.0 36.00  4.00    0    -2.75 2.00
-----------------------------------------------------------------------
group: C
vars n  mean   sd median trimmed  mad  min   max range skew kurtosis   se
Machine_name*    1 2  3.00 0.00   3.00    3.00 0.00  3.0  3.00  0.00  NaN      NaN 0.00
Pressure         2 2 71.91 0.30  71.91   71.91 0.31 71.7 72.12  0.42    0    -2.75 0.21
Temperature      3 2 33.50 3.54  33.50   33.50 3.71 31.0 36.00  5.00    0    -2.75 2.50
-----------------------------------------------------------------------
group: D
vars n  mean   sd median trimmed  mad   min   max range skew kurtosis   se
Machine_name*    1 2  4.00 0.00   4.00    4.00 0.00  4.00  4.00  0.00  NaN      NaN 0.00
Pressure         2 2 77.03 4.50  77.03   77.03 4.71 73.85 80.21  6.36    0    -2.75 3.18
Temperature      3 2 36.00 2.83  36.00   36.00 2.97 34.00 38.00  4.00    0    -2.75 2.00


As the output shows descriptive statistics for dataframe which is group by Machine_name column of dataframe.