R: How to split a data frame into training, validation, and test sets?

Viewed 68156

I'm using R to do machine learning. Following standard machine learning methodology, I would like to randomly split my data into training, validation, and test data sets. How do I do that in R?

I know there are some related questions on how to split into 2 data sets (e.g. this post), but it is not obvious how to do it for 3 split data sets. By the way, the correct approach is to use 3 data sets (including a validation set to tune your hyperparameters).

7 Answers

Caret also support data splitting with the function createDataPartition

if your outcome y is Unbalanced factor ( yes >>> No and vice versa), ideally the random sampling occurs within each class and should preserve the overall class distribution of the data. which is the case with createDataPartition

Example:

library(caret)
set.seed(123)
table(iris$Species=="setosa")
## 
## FALSE  TRUE 
##   100    50

Note our outcome is unbalanced

Splitting (80% train and 20% test):

trainIndex <- createDataPartition(iris$Species=="setosa", p = .8, 
                                  list = FALSE, 
                                  times = 1)
train = iris[ trainIndex,]
test = iris[-trainIndex,]

Verification:

table(train$Species == "setosa")

## 
## FALSE  TRUE 
##    80    40
table(test$Species == "setosa")
## 
## FALSE  TRUE 
##    20    10

Note we preserve the overall class distribution

Related