> 文档中心 > 统计学习导论之R语言应用(二):R语言基础

统计学习导论之R语言应用(二):R语言基础


统计学习导论(ISLR)


参考资料

The Elements of Statistical Learning
An Introduction to Statistical Learning
统计学习导论(二):统计学习概述
统计学习导论(三):线性回归
统计学习导论(四):分类
统计学习导论之R语言应用(二):R语言基础
统计学习导论之R语言应用(三):线性回归R语言代码实战
统计学习导论之R语言应用(四):分类算法R语言代码实战

文章目录

  • 统计学习导论(ISLR)
    • 1.1 基础命令
    • 1.2 画图
    • 1.3数据索引
    • 1.4 数据导入
    • 1.5 绘图和摘要

—# 1、统计学习库——R语言介绍

1.1 基础命令

R使用函数来执行操作。要运行一个名为funcname的函数我们输入funcname(input1, input2)
,其中的输入(或参数)input1并input2告诉R如何运行该函数。一个函数可以有任意数量的输入。
例如,要创建一个数字向量,我们使用函数c()(用于concatenate)。括号内的任何数字都连接在一起。
以下命令指示R将数字 1、3、2 和 5 连接在一起,并将它们保存为名为 的向量x。当我们输入时x,它会给我们返回向量。

x <- c(1, 3, 2)x
  1. 1
  2. 3
  3. 2
y <- c(1, 4, 3)y
  1. 1
  2. 4
  3. 3
length(x)

3

length(y)

3

x + y
  1. 2
  2. 7
  3. 5

该ls()函数允许我们查看迄今为止保存的所有对象(例如数据和函数)的列表。该rm()函数可用于删除我们不想要的任何内容。

ls()
  1. 'x'
  2. 'y'
rm(x,y)ls()

matrix()函数可用于创建数字矩阵。在我们使用该matrix()函数之前,我们可以了解更多信息

x <- matrix(data = c(1, 2, 3, 4), nrow = 2, ncol = 2)x
A matrix: 2 × 2 of type dbl
1 3
2 4

有时指定传入参数的名称会很有用,否则R将假定函数参数以函数帮助文件中给出的相同顺序传递到函数中。如本示例所示,默认情况下R通过连续填充列来创建矩阵。或者,该byrow = TRUE选项可用于按行的顺序填充矩阵。

matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE)
A matrix: 2 × 2 of type dbl
1 2
3 4

请注意,在上面的命令中,我们没有将矩阵分配给诸如的值x。在这种情况下,矩阵会打印到屏幕上,但不会保存以供将来计算。该sqrt()函数返回向量或矩阵的每个元素的平方根。

x^2
A matrix: 2 × 2 of type dbl
1 9
4 16

rnorm()函数生成一个随机正态变量向量,第一个参数n是样本大小。每次我们调用这个函数时,都会得到不同的答案。在这里,我们创建了两组相关的数字x和y,并使用该cor()函数计算它们之间的相关性。

x <- rnorm(50)y <- x + rnorm(50, mean = 50, sd = .1)cor(x, y)

0.994116841106629

默认情况下,rnorm()创建标准正态随机变量,均值为0 和标准差 1. 但是,可以使用mean和sd参数更改平均值和标准差,如上所示。有时我们希望我们的代码重现完全相同的随机数集;我们可以使用该set.seed()函数来做到这一点。该set.seed()函数采用(任意)整数参数。

set.seed(1303)rnorm(50)
  1. -1.14397631447974
  2. 1.34212936561501
  3. 2.18539047574276
  4. 0.536392517923731
  5. 0.0631929664685468
  6. 0.502234482468979
  7. -0.000416724686432643
  8. 0.565819840539162
  9. -0.572522688962623
  10. -1.11022500727696
  11. -0.0486871233624514
  12. -0.695656217619366
  13. 0.828917480303335
  14. 0.206652855081802
  15. -0.235674509102427
  16. -0.556310491381104
  17. -0.364754357080585
  18. 0.862355034263622
  19. -0.63077153536771
  20. 0.313602125215739
  21. -0.931495317661393
  22. 0.823867618473952
  23. 0.523370702077482
  24. 0.706921411979056
  25. 0.420204325601679
  26. -0.269052154682033
  27. -1.51031729990999
  28. -0.69021247657504
  29. -0.143471952443572
  30. -1.0135274099044
  31. 1.57327373614751
  32. 0.0127465054882014
  33. 0.872647049887217
  34. 0.422066190530336
  35. -0.0188157916578866
  36. 2.61574896890584
  37. -0.693140174826871
  38. -0.266321780991085
  39. -0.720636441231524
  40. 1.36773420645149
  41. 0.264007332160512
  42. 0.632186807367191
  43. -1.33065098578719
  44. 0.0268888182209596
  45. 1.0406363207788
  46. 1.31202379854711
  47. -0.0300020766733214
  48. -0.250025712488174
  49. 0.0234144856913592
  50. 1.65987065574227
set.seed(3)y <- rnorm(100)mean(y)var(y)sqrt(var(y))sd(y)

0.0110355710943715

0.732867501277449

0.856076808047881

0.856076808047881

1.2 画图

plot()函数例如,plot(x, y)生成 中的数字x与 中的数字的散点图y。
有许多附加选项可以传递给plot()函数。例如,传入参数xlab将导致标签上的X-轴。要了解有关该plot()函数的更多信息,
请键入?plot。

x <- rnorm(100)y <- rnorm(100)plot(x, y)

在这里插入图片描述

plot(x, y, xlab = "this is the x-axis",    ylab = "this is the y-axis",    main = "Plot of X vs Y")

在这里插入图片描述

pdf("Figure.pdf")plot(x, y, col = "green")dev.off()

png: 2

seq 函数生成序列,seq(a,b)生成a到b的一个序列,默认步长为1,可以用length定义序列长度,by定义步长

seq(1,10, by = 0.5)
  1. 1
  2. 1.5
  3. 2
  4. 2.5
  5. 3
  6. 3.5
  7. 4
  8. 4.5
  9. 5
  10. 5.5
  11. 6
  12. 6.5
  13. 7
  14. 7.5
  15. 8
  16. 8.5
  17. 9
  18. 9.5
  19. 10
?seq
x <- seq(-pi, pi, length = 50)
x
  1. -3.14159265358979
  2. -3.0133643820147
  3. -2.88513611043961
  4. -2.75690783886451
  5. -2.62867956728942
  6. -2.50045129571433
  7. -2.37222302413923
  8. -2.24399475256414
  9. -2.11576648098904
  10. -1.98753820941395
  11. -1.85930993783886
  12. -1.73108166626376
  13. -1.60285339468867
  14. -1.47462512311358
  15. -1.34639685153848
  16. -1.21816857996339
  17. -1.0899403083883
  18. -0.961712036813202
  19. -0.833483765238109
  20. -0.705255493663015
  21. -0.577027222087922
  22. -0.448798950512828
  23. -0.320570678937734
  24. -0.192342407362641
  25. -0.064114135787547
  26. 0.0641141357875465
  27. 0.19234240736264
  28. 0.320570678937734
  29. 0.448798950512828
  30. 0.577027222087921
  31. 0.705255493663015
  32. 0.833483765238108
  33. 0.961712036813202
  34. 1.0899403083883
  35. 1.21816857996339
  36. 1.34639685153848
  37. 1.47462512311358
  38. 1.60285339468867
  39. 1.73108166626376
  40. 1.85930993783886
  41. 1.98753820941395
  42. 2.11576648098904
  43. 2.24399475256414
  44. 2.37222302413923
  45. 2.50045129571433
  46. 2.62867956728942
  47. 2.75690783886451
  48. 2.88513611043961
  49. 3.0133643820147
  50. 3.14159265358979

我们现在将创建一些更复杂的图。该contour()函数产生一个以表示三维数据;它就像一张地形图。它需要三个参数:

  • x值的向量(第一维),
  • y值的向量(第二维),
  • 以及一个矩阵,其元素对应z于每对 ( x, y) 坐标的值(第三维)。

与plot()函数一样,还有许多其他输入可用于微调contour()函数的输出。要了解有关这些的更多信息,请通过键入查看帮助文件?contour。

y <- xf <- outer(x, y, function(x, y) cos(y) / (1 + x^2))contour(x, y, f)contour(x, y, f, nlevels = 45, add = T)

在这里插入图片描述

fa <- (f - t(f)) / 2contour(x, y, fa, nlevels = 15)

在这里插入图片描述

image()函数的工作方式与 相同contour(),不同之处在于它生成一个颜色编码的图,其颜色取决于z值。有时用于在天气预报中绘制温度。或者,persp()可用于生成三维图。参数theta和phi控制查看绘图的角度。

image(x, y, fa)

在这里插入图片描述

persp(x, y, fa)

在这里插入图片描述

persp(x, y, fa, theta = 30)

在这里插入图片描述

persp(x, y, fa, theta = 30, phi = 20)

在这里插入图片描述

1.3数据索引

A <- matrix(1:16, 4, 4)A
A matrix: 4 × 4 of type int
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
A[2, 3]

10

将选择第二行第三列对应的元素。开括号符号后的第一个数字[始终表示行,第二个数字始终表示列。通过提供向量作为索引,我们还可以一次选择多行和多列。

A[c(1, 3), c(2, 4)]
A matrix: 2 × 2 of type int
5 13
7 15
A[1:3, 2:4]
A matrix: 3 × 3 of type int
5 9 13
6 10 14
7 11 15
A[1:2, ]
A matrix: 2 × 4 of type int
1 5 9 13
2 6 10 14
A[, 1:2]
A matrix: 4 × 2 of type int
1 5
2 6
3 7
4 8

在索引中使用负号,表示保留除索引中指示的行或列之外的所有行或列。

A[-c(1, 3), ]
A matrix: 2 × 4 of type int
2 6 10 14
4 8 12 16

1.4 数据导入

对于大多数分析,第一步将数据集导入R. read.table()函数是执行此操作的主要方法之一。帮助文件包含有关如何使用此功能的详细信息。我们可以使用该函数write.table()导出数据。

在尝试加载数据集之前,我们必须确保R知道在正确的目录中搜索数据。例如,在 Windows 系统上,可以使用菜单Change dir …下的选项选择目录File。但是,如何执行此操作的详细信息取决于所使用的操作系统(例如 Windows、Mac、Unix),因此我们在此不提供更多详细信息。

我们首先加载Auto数据集。该数据是ISLR2库的一部分,在第 3 章中讨论过。为了说明该read.table()功能,我们现在从一个文本文件 加载它Auto.data,您可以在教科书网站上找到该文件。下面的命令会将Auto.data文件加载到R一个名为 的对象中,并将其存储为一个名为Auto. 加载数据后,View()可以使用该函数在类似电子表格的窗口中查看数据。(此功能有时可能有点挑剔。如果您在使用它时遇到问题,请尝试使用该head()功能。) 该head()功能还可用于查看数据的前几行。

Auto <- read.table("C:\\Users\\DELL\\Desktop\\Statistic learning\\ISLR\\ALL+CSV+FILES+-+2nd+Edition+-+corrected\\ALL CSV FILES - 2nd Edition\\Auto.data")View(Auto)head(Auto)
A data.frame: 398 × 9
V1 V2 V3 V4 V5 V6 V7 V8 V9
mpg cylinders displacement horsepower weight acceleration year origin name
18.0 8 307.0 130.0 3504. 12.0 70 1 chevrolet chevelle malibu
15.0 8 350.0 165.0 3693. 11.5 70 1 buick skylark 320
18.0 8 318.0 150.0 3436. 11.0 70 1 plymouth satellite
16.0 8 304.0 150.0 3433. 12.0 70 1 amc rebel sst
17.0 8 302.0 140.0 3449. 10.5 70 1 ford torino
15.0 8 429.0 198.0 4341. 10.0 70 1 ford galaxie 500
14.0 8 454.0 220.0 4354. 9.0 70 1 chevrolet impala
14.0 8 440.0 215.0 4312. 8.5 70 1 plymouth fury iii
14.0 8 455.0 225.0 4425. 10.0 70 1 pontiac catalina
15.0 8 390.0 190.0 3850. 8.5 70 1 amc ambassador dpl
15.0 8 383.0 170.0 3563. 10.0 70 1 dodge challenger se
14.0 8 340.0 160.0 3609. 8.0 70 1 plymouth 'cuda 340
15.0 8 400.0 150.0 3761. 9.5 70 1 chevrolet monte carlo
14.0 8 455.0 225.0 3086. 10.0 70 1 buick estate wagon (sw)
24.0 4 113.0 95.00 2372. 15.0 70 3 toyota corona mark ii
22.0 6 198.0 95.00 2833. 15.5 70 1 plymouth duster
18.0 6 199.0 97.00 2774. 15.5 70 1 amc hornet
21.0 6 200.0 85.00 2587. 16.0 70 1 ford maverick
27.0 4 97.00 88.00 2130. 14.5 70 3 datsun pl510
26.0 4 97.00 46.00 1835. 20.5 70 2 volkswagen 1131 deluxe sedan
25.0 4 110.0 87.00 2672. 17.5 70 2 peugeot 504
24.0 4 107.0 90.00 2430. 14.5 70 2 audi 100 ls
25.0 4 104.0 95.00 2375. 17.5 70 2 saab 99e
26.0 4 121.0 113.0 2234. 12.5 70 2 bmw 2002
21.0 6 199.0 90.00 2648. 15.0 70 1 amc gremlin
10.0 8 360.0 215.0 4615. 14.0 70 1 ford f250
10.0 8 307.0 200.0 4376. 15.0 70 1 chevy c20
11.0 8 318.0 210.0 4382. 13.5 70 1 dodge d200
9.0 8 304.0 193.0 4732. 18.5 70 1 hi 1200d
... ... ... ... ... ... ... ... ...
28.0 4 112.0 88.00 2605. 19.6 82 1 chevrolet cavalier
27.0 4 112.0 88.00 2640. 18.6 82 1 chevrolet cavalier wagon
34.0 4 112.0 88.00 2395. 18.0 82 1 chevrolet cavalier 2-door
31.0 4 112.0 85.00 2575. 16.2 82 1 pontiac j2000 se hatchback
29.0 4 135.0 84.00 2525. 16.0 82 1 dodge aries se
27.0 4 151.0 90.00 2735. 18.0 82 1 pontiac phoenix
24.0 4 140.0 92.00 2865. 16.4 82 1 ford fairmont futura
36.0 4 105.0 74.00 1980. 15.3 82 2 volkswagen rabbit l
37.0 4 91.00 68.00 2025. 18.2 82 3 mazda glc custom l
31.0 4 91.00 68.00 1970. 17.6 82 3 mazda glc custom
38.0 4 105.0 63.00 2125. 14.7 82 1 plymouth horizon miser
36.0 4 98.00 70.00 2125. 17.3 82 1 mercury lynx l
36.0 4 120.0 88.00 2160. 14.5 82 3 nissan stanza xe
36.0 4 107.0 75.00 2205. 14.5 82 3 honda accord
34.0 4 108.0 70.00 2245 16.9 82 3 toyota corolla
38.0 4 91.00 67.00 1965. 15.0 82 3 honda civic
32.0 4 91.00 67.00 1965. 15.7 82 3 honda civic (auto)
38.0 4 91.00 67.00 1995. 16.2 82 3 datsun 310 gx
25.0 6 181.0 110.0 2945. 16.4 82 1 buick century limited
38.0 6 262.0 85.00 3015. 17.0 82 1 oldsmobile cutlass ciera (diesel)
26.0 4 156.0 92.00 2585. 14.5 82 1 chrysler lebaron medallion
22.0 6 232.0 112.0 2835 14.7 82 1 ford granada l
32.0 4 144.0 96.00 2665. 13.9 82 3 toyota celica gt
36.0 4 135.0 84.00 2370. 13.0 82 1 dodge charger 2.2
27.0 4 151.0 90.00 2950. 17.3 82 1 chevrolet camaro
27.0 4 140.0 86.00 2790. 15.6 82 1 ford mustang gl
44.0 4 97.00 52.00 2130. 24.6 82 2 vw pickup
32.0 4 135.0 84.00 2295. 11.6 82 1 dodge rampage
28.0 4 120.0 79.00 2625. 18.6 82 1 ford ranger
31.0 4 119.0 82.00 2720. 19.4 82 1 chevy s-10
A data.frame: 6 × 9
V1 V2 V3 V4 V5 V6 V7 V8 V9
1 mpg cylinders displacement horsepower weight acceleration year origin name
2 18.0 8 307.0 130.0 3504. 12.0 70 1 chevrolet chevelle malibu
3 15.0 8 350.0 165.0 3693. 11.5 70 1 buick skylark 320
4 18.0 8 318.0 150.0 3436. 11.0 70 1 plymouth satellite
5 16.0 8 304.0 150.0 3433. 12.0 70 1 amc rebel sst
6 17.0 8 302.0 140.0 3449. 10.5 70 1 ford torino

此特定数据集未正确加载,因为R已假定变量名称是数据的一部分,因此已将它们包含在第一行中。数据集还包括许多缺失的观察值,用问号 表示?。缺失值在真实数据集中很常见。使用选项header = T(或header = TRUE在)read.table()函数告知R该文件的第一行包含变量名称,并使用选项na.strings告诉R任何时候它看到一个特定的字符或字符集(如问号)的,它应该是被视为数据矩阵的缺失元

该stringsAsFactors = T参数告诉我们R,任何包含字符串的变量都应被解释为定性变量,并且每个不同的字符串代表该定性变量的不同级别。从 Excel 加载数据的一种简单方法R是将其保存为 csv(逗号分隔值)文件,然后使用该read.csv()函数。

Auto <- read.table("C:\\Users\\DELL\\Desktop\\Statistic learning\\ISLR\\ALL+CSV+FILES+-+2nd+Edition+-+corrected\\ALL CSV FILES - 2nd Edition\\Auto.data",header = T, na.strings = "?", stringsAsFactors = T)View(Auto)
A data.frame: 397 × 9
mpg cylinders displacement horsepower weight acceleration year origin name
18 8 307 130 3504 12.0 70 1 chevrolet chevelle malibu
15 8 350 165 3693 11.5 70 1 buick skylark 320
18 8 318 150 3436 11.0 70 1 plymouth satellite
16 8 304 150 3433 12.0 70 1 amc rebel sst
17 8 302 140 3449 10.5 70 1 ford torino
15 8 429 198 4341 10.0 70 1 ford galaxie 500
14 8 454 220 4354 9.0 70 1 chevrolet impala
14 8 440 215 4312 8.5 70 1 plymouth fury iii
14 8 455 225 4425 10.0 70 1 pontiac catalina
15 8 390 190 3850 8.5 70 1 amc ambassador dpl
15 8 383 170 3563 10.0 70 1 dodge challenger se
14 8 340 160 3609 8.0 70 1 plymouth 'cuda 340
15 8 400 150 3761 9.5 70 1 chevrolet monte carlo
14 8 455 225 3086 10.0 70 1 buick estate wagon (sw)
24 4 113 95 2372 15.0 70 3 toyota corona mark ii
22 6 198 95 2833 15.5 70 1 plymouth duster
18 6 199 97 2774 15.5 70 1 amc hornet
21 6 200 85 2587 16.0 70 1 ford maverick
27 4 97 88 2130 14.5 70 3 datsun pl510
26 4 97 46 1835 20.5 70 2 volkswagen 1131 deluxe sedan
25 4 110 87 2672 17.5 70 2 peugeot 504
24 4 107 90 2430 14.5 70 2 audi 100 ls
25 4 104 95 2375 17.5 70 2 saab 99e
26 4 121 113 2234 12.5 70 2 bmw 2002
21 6 199 90 2648 15.0 70 1 amc gremlin
10 8 360 215 4615 14.0 70 1 ford f250
10 8 307 200 4376 15.0 70 1 chevy c20
11 8 318 210 4382 13.5 70 1 dodge d200
9 8 304 193 4732 18.5 70 1 hi 1200d
27 4 97 88 2130 14.5 71 3 datsun pl510
... ... ... ... ... ... ... ... ...
28 4 112 88 2605 19.6 82 1 chevrolet cavalier
27 4 112 88 2640 18.6 82 1 chevrolet cavalier wagon
34 4 112 88 2395 18.0 82 1 chevrolet cavalier 2-door
31 4 112 85 2575 16.2 82 1 pontiac j2000 se hatchback
29 4 135 84 2525 16.0 82 1 dodge aries se
27 4 151 90 2735 18.0 82 1 pontiac phoenix
24 4 140 92 2865 16.4 82 1 ford fairmont futura
36 4 105 74 1980 15.3 82 2 volkswagen rabbit l
37 4 91 68 2025 18.2 82 3 mazda glc custom l
31 4 91 68 1970 17.6 82 3 mazda glc custom
38 4 105 63 2125 14.7 82 1 plymouth horizon miser
36 4 98 70 2125 17.3 82 1 mercury lynx l
36 4 120 88 2160 14.5 82 3 nissan stanza xe
36 4 107 75 2205 14.5 82 3 honda accord
34 4 108 70 2245 16.9 82 3 toyota corolla
38 4 91 67 1965 15.0 82 3 honda civic
32 4 91 67 1965 15.7 82 3 honda civic (auto)
38 4 91 67 1995 16.2 82 3 datsun 310 gx
25 6 181 110 2945 16.4 82 1 buick century limited
38 6 262 85 3015 17.0 82 1 oldsmobile cutlass ciera (diesel)
26 4 156 92 2585 14.5 82 1 chrysler lebaron medallion
22 6 232 112 2835 14.7 82 1 ford granada l
32 4 144 96 2665 13.9 82 3 toyota celica gt
36 4 135 84 2370 13.0 82 1 dodge charger 2.2
27 4 151 90 2950 17.3 82 1 chevrolet camaro
27 4 140 86 2790 15.6 82 1 ford mustang gl
44 4 97 52 2130 24.6 82 2 vw pickup
32 4 135 84 2295 11.6 82 1 dodge rampage
28 4 120 79 2625 18.6 82 1 ford ranger
31 4 119 82 2720 19.4 82 1 chevy s-10
Auto[1:4, ]
A data.frame: 4 × 9
mpg cylinders displacement horsepower weight acceleration year origin name
1 18 8 307 130 3504 12.0 70 1 chevrolet chevelle malibu
2 15 8 350 165 3693 11.5 70 1 buick skylark 320
3 18 8 318 150 3436 11.0 70 1 plymouth satellite
4 16 8 304 150 3433 12.0 70 1 amc rebel sst
dim(Auto)
  1. 397
  2. 9

该dim()函数告诉我们数据有397观察值或行,以及九个变量或列。有多种方法可以处理丢失的数据。在这种情况下,只有五行包含缺失的观察,因此我们选择使用该na.omit()函数来简单地删除这些行。

Auto <- na.omit(Auto)dim(Auto)
  1. 392
  2. 9
names(Auto)
  1. 'mpg'
  2. 'cylinders'
  3. 'displacement'
  4. 'horsepower'
  5. 'weight'
  6. 'acceleration'
  7. 'year'
  8. 'origin'
  9. 'name'

1.5 绘图和摘要

plot(Auto$cylinders, Auto$mpg)

在这里插入图片描述

attach(Auto)
cylinders <- as.factor(cylinders)
plot(cylinders, mpg)

在这里插入图片描述

plot(cylinders, mpg, col = "red")

在这里插入图片描述

plot(cylinders, mpg, col = "red", varwidth = T,    horizontal = T)

在这里插入图片描述

plot(cylinders, mpg, col = "red", varwidth = T,    xlab = "cylinders", ylab = "MPG")

在这里插入图片描述

hist(mpg)

在这里插入图片描述

hist(mpg, col = 2, breaks = 15)

在这里插入图片描述

pairs()函数创建一个散点图矩阵,即每对变量的散点图。我们还可以为变量的一个子集生成散点图。

pairs(Auto)

在这里插入图片描述

pairs(    ~ mpg + displacement + horsepower + weight + acceleration,    data = Auto  )

在这里插入图片描述

与该plot()函数结合使用,identify()提供了一种有用的交互方法,用于识别图上点的特定变量的值。
我们将三个参数传递给identify():X-axis 变量, 是-axis 变量,以及我们希望为每个点打印其值的变量。然后单击图中的一个或多个点并按 Escape 将R打印出感兴趣的变量的值。
在identify()函数下打印的数字对应于所选点的行。

plot(horsepower, mpg)identify(horsepower, mpg, name)

在这里插入图片描述

plot(horsepower, mpg)

在这里插入图片描述

summary(Auto)
      mpg   cylinders      displacement     horsepower weight     Min.   : 9.00   Min.   :3.000   Min.   : 68.0   Min.   : 46.0   Min.   :1613   1st Qu.:17.00   1st Qu.:4.000   1st Qu.:105.0   1st Qu.: 75.0   1st Qu.:2225   Median :22.75   Median :4.000   Median :151.0   Median : 93.5   Median :2804   Mean   :23.45   Mean   :5.472   Mean   :194.4   Mean   :104.5   Mean   :2978   3rd Qu.:29.00   3rd Qu.:8.000   3rd Qu.:275.8   3rd Qu.:126.0   3rd Qu.:3615   Max.   :46.60   Max.   :8.000   Max.   :455.0   Max.   :230.0   Max.   :5140      acceleration year    origin name     Min.   : 8.00   Min.   :70.00   Min.   :1.000   amc matador:  5   1st Qu.:13.78   1st Qu.:73.00   1st Qu.:1.000   ford pinto :  5   Median :15.50   Median :76.00   Median :1.000   toyota corolla    :  5   Mean   :15.54   Mean   :75.98   Mean   :1.577   amc gremlin:  4   3rd Qu.:17.02   3rd Qu.:79.00   3rd Qu.:2.000   amc hornet :  4   Max.   :24.80   Max.   :82.00   Max.   :3.000   chevrolet chevette:  4  (Other)    :365  
summary(mpg)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    9.00   17.00   22.75   23.45   29.00   46.60