R – Summary of Data Frame
To get the summary of Data Frame, call summary() function and pass the Data Frame as argument to the function. We may pass additional arguments to summary() that affects the summary output.
The output of summary() contains summary for each column. If the column is numeric type, then the summary would contain information like minimum, maximum, median, mean, etc. If the column is char type, then summary would contain information like length, class and mode.
In this tutorial, we will learn how to use summary() function to get the summary of a Data Frame, with the help of examples.
The syntax to call summary() function to get the summary of Data Frame
summary(df, maxsum = 7, digits = max(3, getOption("digits")-3), ...)
|df||R Data Frame|
|maxsum||An integer. It indicates the number of levels to be shown for factors.|
|digits||An integer. It is used for formatting the print output.|
|…||additional arguments affecting the summary produced.|
summary() function returns a matrix object of class “table”.
In the following program, we take a Data Frame in
df with two numeric columns, and find its summary using summary() function.
df <- data.frame(a = c(41, 42, 43), b = c(44, 45, 46)) output <- summary(df) print(output)
a b Min. :41.0 Min. :44.0 1st Qu.:41.5 1st Qu.:44.5 Median :42.0 Median :45.0 Mean :42.0 Mean :45.0 3rd Qu.:42.5 3rd Qu.:45.5 Max. :43.0 Max. :46.0
Output contains summary which is column wise. In the first column named “a”, minimum value is 41.0, maximum value is 43.0, mean 42.0, and so on. Similarly for the second column, the summary would contain values based on its values.
Now, let us take a Data Frame which contains a char column, and find its summary.
df <- data.frame(a = c(41, 42, 43), b = c("apple", "mango", "cherry")) output <- summary(df) print(output)
a b Min. :41.0 Length:3 1st Qu.:41.5 Class :character Median :42.0 Mode :character Mean :42.0 3rd Qu.:42.5 Max. :43.0
The summary for char column contains different values when compared to the numeric column.
In this R Tutorial, we learned to get the summary of an R Data Frame using summary() function, with the help of examples.