Splitting the data into training and testing sets - R

+1 vote

I am working with the 'beaver1' data-set, below is a sample:

   day time  temp activ
1 346  840   36.33   0
2 346  850   36.34   0
3 346  900   36.35   0
4 346  910   36.42   0
5 346  920   36.55   0
6 346  930   36.69   0

I want to split this data into 'train' and 'test' sets with 65:35 ratio so that i can build a machine learning model on top of it, how can i do it?

May 7, 2018 in Data Analytics by DataKing99
• 8,250 points
6,128 views

2 answers to this question.

0 votes

You can use the sample.split() function from the caTools package for this purpose:

Start off by loading the 'caTools' package:

library(caTools)

Then, use the sample.split() function which takes in two parameters -> the dataset - 'beaver1' and SplitRatio - 0.65

sample.split(beaver1,SplitRatio = 0.65)->mysplit

Following which, use the subset() function and select all those observations where 'mysplit' tag is True and store them in 'train'

subset(beaver1,mysplit==T)->train

Similarly, select all those observations where  the 'mysplilt' tag is Fasle, and store them in 'test'

subset(beaver1,mysplit==F)->test

And that's how you split the data into 'train' and 'test' sets.

answered May 7, 2018 by Bharani
• 4,660 points
0 votes

Hi,

Try like this.

train  = sample(x = c(TRUE, FALSE), size = nrow(dataframe),replace = TRUE, prob = c(0.65, 0.35))

Get training and test data frame from the main dataset.

data[train,]
data[!train,]
answered Aug 21, 2019 by anonymous
• 33,030 points

Related Questions In Data Analytics

0 votes
1 answer

How can I list all the data sets available in all R packages?

You can use the below line of ...READ MORE

answered Sep 7, 2018 in Data Analytics by zombie
• 3,790 points
1,010 views
0 votes
1 answer
0 votes
1 answer

How to insert Data into the MySQL Tables using R?

Hi@akhtar, Before you try to insert the data ...READ MORE

answered Oct 29, 2020 in Data Analytics by MD
• 95,460 points
2,818 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
1,180 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
5,890 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
1,113 views
0 votes
1 answer

Plotting multiple graphs on the same page in R

If you want to plot 4 graphs ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
1,426 views
0 votes
2 answers

"Train" and "Test" sets in Data Science

Normally to perform supervised learning you need ...READ MORE

answered Aug 3, 2018 in Data Analytics by Abhi
• 3,720 points
1,325 views
0 votes
1 answer

Applying the same function to every row of a data.frame - R

You can use the 'appply()' function for ...READ MORE

answered May 22, 2018 in Data Analytics by Bharani
• 4,660 points
1,695 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP