Splitting Data into Training and Test Sets with R

In this tutorial, you will learn how to split sample into training and test data sets with R.

The following code splits 70% of the data selected randomly into training set and the remaining 30% sample into test data set.

dt = sort(sample(nrow(data), nrow(data)*.7))

Here sample( ) function randomly picks 70% rows from the data set. It is sampling without replacement.

Method 2 : To maintain same percentage of event rate in both training and validation dataset.
trainIndex <- createDataPartition(data$FD, p = .7,
                                  list = FALSE,
                                  times = 1)
Train <- data[ trainIndex,]
Valid <- data[-trainIndex,]

In the above program, FD is a dependent variable having two values 1 and 0. Make sure it is defined in factor format.

R Tutorials : 75 Free R Tutorials

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like banking, Telecom, HR and Health Insurance.

While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*
Related Posts:
5 Responses to "Splitting Data into Training and Test Sets with R"
  1. This comment has been removed by the author.

  2. This won't randomize the order, bad option

    1. What do you mean by "This won't randomize the order"? Sample function randomize the order.

  3. One more way to split data into two part


    iris <- iris

    iris$spl <-sample.split(iris,SplitRatio = 0.7)
    train=subset(iris, iris$spl==TRUE)
    test <- subset(iris,iris$spl ==FALSE)

    1. This is the correct solution when you have a dependent column. Thank you, Rishabh.


Next → ← Prev