How to split data into train and test in R using caret package in R?

Description

To split the data into train and test in R using caret package in R programming.

  • R Package : caret
  • R Function : createDataPartition(x ,p=
    ,list= )
  • x — a vector of outcomes.
  • p — the percentage of data that
    goes to training
  • List — Logical-should the results
    be in a list (TRUE)

#Test and Split sample
#Loading required Packages
#install.packages(“caret”)
library(“caret”)

#Read the data
library(“xlsx”)
input<-read.xlsx(“mtcars.xlsx”,sheetIndex = 1)
print(input)

#Split Data
set.seed(123)
my_data<-mtcars$mpg

#Train set
my_train<-createDataPartition(my_data,p=0.7,list=FALSE)
train<-input[my_train,]
print(train)

#Test Data
my_test<-createDataPartition(my_data[-my_train],list=FALSE)
test<-input[-my_train,][my_test,]
print(test)

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit