当我运行此代码时:
library(ISLR)
train = (year%%2 == 0)
test = !train
Auto.test = Auto[train,]
Auto.test
我有以下输出(这三个点是我给的):
mpg cylinders displacement horsepower weight acceleration year origin name
1 18.0 8 307.0 130 3504 12.0 70 1 chevrolet chevelle malibu
2 15.0 8 350.0 165 3693 11.5 70 1 buick skylark 320
3 18.0 8 318.0 150 3436 11.0 70 1 plymouth satellite
4 16.0 8 304.0 150 3433 12.0 70 1 amc rebel sst
5 17.0 8 302.0 140 3449 10.5 70 1 ford torino
...
199 33.0 4 91.0 53 1795 17.4 76 3 honda civic
200 20.0 6 225.0 100 3651 17.7 76 1 dodge aspen se
201 18.0 6 250.0 78 3574 21.0 76 1 ford granada ghia
202 18.5 6 250.0 110 3645 16.2 76 1 pontiac ventura sj
203 17.5 6 258.0 95 3193 17.8 76 1 amc pacer d/l
204 29.5 4 97.0 71 1825 12.2 76 2 volkswagen rabbit
205 32.0 4 85.0 70 1990 17.0 76 3 datsun b-210
206 28.0 4 97.0 75 2155 16.4 76 3 toyota corolla
207 26.5 4 140.0 72 2565 13.6 76 1 ford pinto
208 20.0 4 130.0 102 3150 15.7 76 2 volvo 245
209 13.0 8 318.0 150 3940 13.2 76 1 plymouth volare premier v8
210 19.0 4 120.0 88 3270 21.9 76 2 peugeot 504
[ reached 'max' / getOption("max.print") -- omitted 99 rows ]
但是当我尝试发现 Auto.test 的尺寸时:
dim(Auto.test)
我有
[1] 210 9
为什么标注命令没有给我这个数据框的真实尺寸? Dimensions命令给了我210行9列。问题是行不正确,在数据框之后,我们应该有210 + 99行
答案 0 :(得分:0)
如果计算输出(“ train.csv”)中的行数,则dim
是正确的。希望以此方式解决问题会消除误会。
options(max.print=1000000)
library(ISLR)
df <- Auto
train <- subset(df, year%%2 == 0)
test <- subset(df, year%%2 != 0)
dim(train)
>[1] 210 9
dim(test)
>[1] 182 9
write.csv(train, file = "train.csv")