In the below example we have: Name of data set = Smarket (this is simulated dataset available in library ISLR) Split ratio = 75% train = name of data set to be used for training purpose test = name of dataset to be used for test purposes
library(ISLR)
attach(Smarket)
smp_siz = floor(0.75*nrow(Smarket)) # creates a value for dividing the data into train and test. In this case the value is defined as 75% of the number of rows in the dataset
smp_siz # shows the value of the sample size
## [1] 937
set.seed(123) # set seed to ensure you always have same random numbers generated
train_ind = sample(seq_len(nrow(Smarket)),size = smp_siz) # Randomly identifies therows equal to sample size ( defined in previous instruction) from all the rows of Smarket dataset and stores the row number in train_ind
train =Smarket[train_ind,] #creates the training dataset with row numbers stored in train_ind
test=Smarket[-train_ind,] # creates the test dataset excluding the row numbers mentioned in train_ind
Name of data set = Smarket (this is simulated dataset available in library ISLR) Split ratio = 75% train1 = name of data set to be used for training purpose test1 = name of dataset to be used for test purposes
This method requires the use of library caTools
require(caTools) # loading caTools library
## Loading required package: caTools
set.seed(123) # set seed to ensure you always have same random numbers generated
sample = sample.split(Smarket,SplitRatio = 0.75) # splits the data in the ratio mentioned in SplitRatio. After splitting marks these rows as logical TRUE and the the remaining are marked as logical FALSE
train1 =subset(Smarket,sample ==TRUE) # creates a training dataset named train1 with rows which are marked as TRUE
test1=subset(Smarket, sample==FALSE)