Question

I have a vector of 300 numbers (from 1 to 300). I want to create two subsets, i.e., model/training (200 numbers) and testing set (100 numbers) with replacement. I tried to use sample and subset but didn't got the results I want.

MWE:
x=(1,2,3,.......300)
x1 = (1,1,2,3,5,5,...........,300) (Consider it training set of 200 samples)
x2 = (1,3,9,101,130,130,..........299)

Any suggestion please !!!!!

Was it helpful?

Solution

You could create a set of random indices for the training set and then select all but those indices for the test set, like this:

data <- c(1,3,8,7,19,5,4,10,11,20)
i <- sample(1:length(data), 5)
training <- data[i]
test <- data[-i]

This will get five points for the training set and all the remaining points will go in the test set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top