How to use Summary Function in R?
In this article, we will discuss the Summary Function in R Programming Language.
Summary function is used to return the following from the given data.
- Min: The minimum value in the given data
- 1st Qu: The value of the 1st quartile (25th percentile) in the given data
- Median: The median value in the given data
- 3rd Qu: The value of the 3rd quartile (75th percentile) in the given data
- Max: The maximum value in the given data
Syntax:
summary(data)
Where, data can be a vector, dataframe, etc.
Here we are going to create a vector with some elements and get the summary statistics.
R
data = c (1: 5, 56, 43, 56, 78, 51)
print (data)
print ( summary (data))
|
Output:
Example 2: Using summary() with DataFrame
Here we are going to get the summary of all columns in the dataframe.
R
data = data.frame (col1= c (1: 5, 56, 43, 56, 78, 51),
col2= c (100: 104, 56, 43, 56, 78, 51),
col3= c (1: 5, 34, 56, 78, 76, 79))
print (data)
print ( summary (data))
|
Output:
Example 3: Using summary() with Specific DataFrame Columns
Here we can get summary of particular columns of the dataframe.
Syntax:
summary(dataframe)
R
data = data.frame (col1= c (1: 5, 56, 43, 56, 78, 51),
col2= c (100: 104, 56, 43, 56, 78, 51),
col3= c (1: 5, 34, 56, 78, 76, 79))
print (data)
print ( summary (data[ c ( 'col1' , 'col3' )]))
|
Output:
Example 4: Using summary() with Regression Model
Here we can also calculate summary() for linear regression model. We can create an linear regression model for dataframe columns using lm() function.
Syntax:
summary(lm(column1~column2, dataframe))
R
data = data.frame (col1= c (1: 5, 56, 43, 56, 78, 51),
col2= c (100: 104, 56, 43, 56, 78, 51))
reg = lm (col1~col2, data)
summary (reg)
|
Output:
Example 5: Using summary() with ANOVA Model
Here aov() is used to create anova model which stands for analysis of variance.
Syntax:
summary(aov(col1 ~ col2, data))
Example:
R
data = data.frame (col1= c (1: 5, 56, 43, 56, 78, 51),
col2= c (100: 104, 56, 43, 56, 78, 51))
reg = aov (col1 ~ col2, data)
summary (reg)
|
Output:
Last Updated :
16 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...