Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)

I've just started using R and I'm not sure how to incorporate my dataset with the following sample code:

sample(x, size, replace = FALSE, prob = NULL)

I have a dataset that I need to put into a training (75%) and testing (25%) set. I'm not sure what information I'm supposed to put into the x and size? Is x the dataset file, and size how many samples I have?

1 Answer

0 votes
by

To split a dataset into a training and a testing set, you can do the following:

To split the mtcars dataset into a training set(75%) and testing set(25%):

data(mtcars)

sample_size <-  floor(0.75 * nrow(mtcars))

mtcars <- mtcars[sample(nrow(mtcars)), ] 

mtcars.train <- mtcars[1:sample_size, ]

mtcars.test <- mtcars[(sample_size+1):nrow(mtcars), ] 

View(mtcars.train)

View(mtcars.test)

Output:

Training set:

image

Test set:

image

Browse Categories

...