📜  如何在 R 中使用汇总函数?

📅  最后修改于: 2022-05-13 01:54:28.723000             🧑  作者: Mango

如何在 R 中使用汇总函数?

在本文中,我们将讨论 R 编程语言中的摘要函数。

摘要函数用于从给定数据中返回以下内容。

  • Min:给定数据中的最小值
  • 1st Qu:给定数据中第一个四分位数(第 25 个百分位数)的值
  • 中值:给定数据中的中值
  • 3rd Qu:给定数据中第三个四分位数(第 75 个百分位数)的值
  • Max:给定数据中的最大值

语法

summary(data)

其中,数据可以是向量、数据框等。

示例 1:将 summary() 与 Vector 一起使用

在这里,我们将创建一个包含一些元素的向量并获取汇总统计信息。

R
# create a vector wit 10 elements
data = c(1: 5, 56, 43, 56, 78, 51)
  
# display
print(data)
  
# get summary
print(summary(data))


R
# create a dataframe with 3 columns
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))
  
# display
print(data)
  
# get summary
print(summary(data))


R
# create a dataframe with 3 columns
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))
  
# display
print(data)
  
# get summary of column 1 and column 3
print(summary(data[c('col1', 'col3')]))


R
# create a dataframe with 3 columns
data = data.frame(col1=c(1: 5, 56, 43, 56, 78, 51),
                  col2=c(100: 104, 56, 43, 56, 78, 51))
  
# create the model for regression with 2 columns
reg = lm(col1~col2, data)
  
# get summary of the model
summary(reg)


R
# create a dataframe with 3 columns
data = data.frame(col1=c(1: 5, 56, 43, 56, 78, 51),
                  col2=c(100: 104, 56, 43, 56, 78, 51))
  
# create the model for anova model with 2 columns
reg = aov(col1 ~ col2, data)
  
# get summary of the model
summary(reg)


输出

示例 2:将 summary() 与 DataFrame 一起使用

在这里,我们将获取数据框中所有列的摘要。

R

# create a dataframe with 3 columns
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))
  
# display
print(data)
  
# get summary
print(summary(data))

输出

示例 3:对特定 DataFrame 列使用 summary()

在这里,我们可以获得数据框特定列的摘要。

语法

summary(dataframe)

R

# create a dataframe with 3 columns
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))
  
# display
print(data)
  
# get summary of column 1 and column 3
print(summary(data[c('col1', 'col3')]))

输出

示例 4:将 summary() 与回归模型一起使用

这里我们还可以计算线性回归模型的summary()。我们可以使用 lm()函数为数据框列创建线性回归模型。

语法

summary(lm(column1~column2, dataframe))

R

# create a dataframe with 3 columns
data = data.frame(col1=c(1: 5, 56, 43, 56, 78, 51),
                  col2=c(100: 104, 56, 43, 56, 78, 51))
  
# create the model for regression with 2 columns
reg = lm(col1~col2, data)
  
# get summary of the model
summary(reg)

输出

示例 5:将 summary() 与 ANOVA 模型一起使用

这里 aov() 用于创建代表方差分析的方差分析模型。

语法

summary(aov(col1 ~ col2, data))

示例

R

# create a dataframe with 3 columns
data = data.frame(col1=c(1: 5, 56, 43, 56, 78, 51),
                  col2=c(100: 104, 56, 43, 56, 78, 51))
  
# create the model for anova model with 2 columns
reg = aov(col1 ~ col2, data)
  
# get summary of the model
summary(reg)

输出