Cbind无效的下标类型' list'在数据集上

时间:2017-02-03 20:02:09

标签: r testing dataframe

我尝试实施 Knn ,但在尝试设置我的列车数据时,我收到以下错误:

  

无效的下标类型'列表'

这是我的代码:

rm(list=ls())
library(class)

library(ISLR)

train = subset(Weekly, Year >= 1990)
test = subset(Weekly, Year >= 2009)
Weekly.Lag1 = Weekly[,c("Lag1")]
Weekly.Lag2 = Weekly[,c("Lag2")]

train.X <- cbind(Weekly.Lag1,Weekly.Lag2)[train,]

我做错了什么,如何解决?

1 个答案:

答案 0 :(得分:1)

rm(list=ls())
library(class)

library(ISLR)

train = which(Weekly$Year >= 1990) # you have to create vector not subset
test = which(Weekly$Year >= 2009) # you have to create vector not subset
Weekly.Lag1 = Weekly[,c("Lag1")]
Weekly.Lag2 = Weekly[,c("Lag2")]

train.X <- cbind(Weekly.Lag1,Weekly.Lag2)[train,]