I am trying to subset a data set with 10 variables and 10000 observations into a training set and testing set so I can create a logistic regression model. I create the length of what the training set will be and the length of what the testing set will be.
data(optiva)
n <- length(optiva$Age)
ntrain <- n*.70
ntest <- n*.30
# Random sample the data set to build the model
train <- optiva[sample(1:n, ntrain, replace=FALSE),]
test <- optiva[-train, ]
Creating the training set works just fine, but when I run the last line to try and create the testing set, I get an error message that says:
Error in xj[i] : invalid subscript type 'list'
I tried changing to code to
test <- optiva[!train, ]
and I get a testing set with over 37 thousand observations, not 3000. I've looked at how to subset data and tried to follow along. Why is it not working for me?