#5, First Floor, 4th Street , Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

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

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

Process

   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)

Sapmle Code

#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)

Screenshots