【发布时间】:2020-04-22 02:31:25
【问题描述】:
请查看下面的代码块 - 本质上,我使用的是 ISLR 库中的 Auto 数据集,缩放数据帧的定量预测变量,随机抽取数据帧样本,然后输出刚刚采样的行。当我运行此代码并尝试访问数据帧时,结果行与“s”向量提供的行值偏移 1-5 之间。是什么导致了这些偏移?我的印象是,通过调用“s”向量指定的行,结果输出应该只包含“s”索引指定的那些行。
请告诉我你的想法,谢谢!
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
mpg01
Mode :logical
FALSE:207
TRUE :185
> AutoScale = scale (Auto[,-c(8,9,10)])
> s = sample (nrow (AutoScale), 10)
> s
[1] 354 1 233 85 163 171 216 297 137 92
> AutoScale [s, ]
mpg cylinders displacement horsepower weight acceleration year
359 1.04472438 -0.8629108 -0.7110965 -0.7915944 -0.40332370 0.9999309 1.3628576
1 -0.69774672 1.4820530 1.0759146 0.6632851 0.61974833 -1.2836176 -1.6232409
235 0.13505197 -0.8629108 -0.4148541 -0.4278746 -0.27970740 0.1662545 0.2770036
86 -1.33836110 1.4820530 1.4868316 1.8323847 1.32141798 -0.9211496 -0.8088504
165 -0.31337809 0.3095711 0.3496427 0.1436853 0.07230472 -0.1962136 -0.2659234
173 0.19911341 -0.8629108 -0.9977828 -0.8695344 -0.88837051 0.3474885 -0.2659234
218 0.83972778 -0.8629108 -0.7971024 -0.6357145 -0.96842678 -0.2687072 0.2770036
299 -0.05713234 1.4820530 1.4868316 0.5333851 1.08595837 0.6737097 0.8199306
139 -1.21023822 1.4820530 1.1810329 1.1828849 1.74171339 -0.7399156 -0.5373869
93 -1.33836110 1.4820530 1.4963878 1.3907248 1.63104738 -0.9211496 -0.8088504
【问题讨论】: