R summary() Function - View Data Summary

R 语言实例R Language Example

The R summary() function is used to generate a statistical summary of data, and is the first step in data exploration.

For numeric data, summary() returns the minimum, quartiles, median, mean, maximum, etc.

The syntax of the summary() function is as follows:

summary(object)

Parameter description:

  • objectInput object, can be a vector, data frame, matrix, or model result.

Example

# Summary of numeric vector
scores <- c(55, 62, 68, 70, 72, 75, 78, 80, 82, 85,
            88, 90, 92, 95, 98)
print("Numeric vector summary:")
print(summary(scores))

# Summary of data frame
df <- data.frame(
Name= c("Zhang San", "Li Si", "Wang Wu", "Zhao Liu", "Qian Qi"),
Age= c(25, 30, 28, 35, 22),
Score= c(88, 92, 76, 85, 90)
)
print("Data frame summary:")
print(summary(df))

Executing the above code outputs the following result:

[1] "数值向量 summary:"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  55.00   71.00   80.00   79.33   88.75   98.00
[1] "数据框 summary:"
     姓名         年龄          成绩
 张三:1   Min.   :22.0   Min.   :76.0
 李四:1   1st Qu.:25.0   1st Qu.:85.0
 王五:1   Median :28.0   Median :88.0
 赵六:1   Mean   :28.0   Mean   :86.2
 钱七:1   3rd Qu.:30.0   3rd Qu.:90.0
          Max.   :35.0   Max.   :92.0

For factor variables, summary() displays frequency statistics; for linear models, it displays detailed model information.

R 语言实例R Language Example

Other Extensions