0 votes
1 view
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 (25.4k points)

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%):


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), ] 




Training set:


Test set:


Welcome to Intellipaat Community. Get your technical queries answered by top developers !