2

I have randomly generated a submatrix “train” from “mat” using the following:

train <- mat[sample(nrow(mat), size = 317, replace = FALSE),] 

My question is, how do I then create “test” as a submatrix of “mat” which excludes the matrix “train”?

Thanks!

TuringTester69
  • 177
  • 1
  • 9
  • You start by assigning `inx <- sample(nrow(mat), size = 317, replace = FALSE)`. Then, `train <- mat[inx, ]` and `test <- mat[-inx, ]`. The negative index *excludes* those rows. – Rui Barradas May 10 '18 at 15:45
  • @RuiBarradas Just posted an answer and I realize it is the same as your comment. This is my first active day on SO, can you point me to when I should comment vs. give answers? – JayCe May 10 '18 at 16:18
  • @JayCe Is your answer usefull to the OP and eventually to others? Then post it. The error is mine, I should have followed this rule and have not. Thanks for pointing this out, I will upvote your answer. – Rui Barradas May 10 '18 at 16:45
  • @RuiBarradas Thanks for the clarification and the upvote - many tenured members also comment instead of answering that's why is was asking. – JayCe May 10 '18 at 16:49

1 Answers1

1

train.index are the indexes used for training.

mat <- matrix(rnorm(20000),nrow=1000)
train.index <-sample(nrow(mat), size = 317, replace = FALSE)
train <- mat[train.index,]
test <- mat[-train.index,]

Try it online!

JayCe
  • 241
  • 3
  • 9