# How to randomly split the data set into multiple different sets:(training 70%:validation 10%(optional):testing 20%) in R?

I have a dataset with 4 predictor variables $$X_1, X_2, X_3, X_4,$$ and one response variable $$Y$$. I have been asked to check the correlation between these variables and see how they are related and then use a linear model to fit them.

No split of training set: test set is given. I have one data set with 10000 samples. I was planning of splitting this data set in a 80:20 ratio for training and testing respectively.

I would like to know how to do the same in the R programming language.

Also in general, we will split it into multiple combinations of training:testing set right? Or? Please correct me if am wrong. I am a newbie to machine learning, so kindly help me out here.

# Load data
library(ISLR)


Split data into two parts (train and "rest"):

# 70% Sample size
smp_size <- floor(0.7 * nrow(cars))
# Set seed to control randomness
set.seed(123)
ind <- sample(seq_len(nrow(cars)), size = smp_size)
# Split data
train <- cars[ind, ]
rest <- cars[-ind, ]


Split "rest" of data again into two sets:

# Split the "rest" of the data into test, val
# 70% Sample size
smp_size <- floor(1/3 * nrow(rest))
# Set seed to control randomness
set.seed(123)
ind <- sample(seq_len(nrow(rest)), size = smp_size)
# Split data
val <- rest[ind, ]
test <- rest[-ind, ]


Usually you choose the ratio between training and test data by how many samples you have. For example, if you do not have that many you would go to something like 90:10, but with a larger one you can even consider something like 75:25, although, 80:20 sounds good for your case.

For the random splitting that you are talking about, you should search and learn a little about k-fold cross-validation. It is a method with which you split your data in training and test sets k times in order to validate your model on different training and test sets each time.

About the R implementation, I cannot really help you, but I am pretty sure it should be quite easy to find a guide on how to do that.

Good luck.

set.seed(1234)
ind <- sample(2, nrow(data1), replace = T, prob = c(0.8,.02))
train <- data1[ind==1,]
test <- data1[ind==2,]