Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Divide data set into 60%, 20%, 20%

I am trying to move from 2 sets of data to 3 sets as explained in the above question. Following is the script I used:

set.seed(125)
d <- sample(x = nrow(db), size = nrow(db) * 0.60, )
train60 <-db[d, ]
valid40 <-db[-d, ]

Is there a way to modify the above script? I have tried to create another line:

valid40 <- db[-d] * 0.2 which did not work.

Current dataset has several factor variables.

I have tried using Frank's solution here on the cut function, but somehow I manage to get

Error in cut.default(seq(nrow(df)), nrow(df) * cumsum(c(0, spec)), labels = names(spec)) : lengths of 'breaks' and 'labels' differ

which I don't understand even after searching for help online.

like image 846
user149635 Avatar asked Mar 10 '23 01:03

user149635


2 Answers

If I understood you correctly then you want a bifurcation of 60%, 20% and 20% of sample without repeat. I have taken iris data for an example which contains 150 rows and 5 columns.

samp <- sample(1:nrow(iris),.6*nrow(iris)) ##60 and 40 bifurcation

train60 <- iris[samp,] ## This is the 60% chunk
remain40 <- iris[-samp,]  ## This is used for further bifurcation

samp2 <- sample(1:nrow(remain40),.5*nrow(remain40))

first20 <- remain40[samp2,] ## First chunk of 20%
secnd20 <- remain40[-samp2,] ## Second Chunk of 20%

Reduce("intersect",list(train60,first20,secnd20)) ##Check to find if there is any intersect , 0 rows means everything is fine and sample are not repetitive.
like image 66
PKumar Avatar answered Mar 30 '23 23:03

PKumar


db <- data.frame(x=1:10, y=11:20)

set.seed(125)
d <- sample(x=nrow(db),size=nrow(db)*0.60,)

train60 <-db[d,]

valid40 <-db[-d,]

Now just take half of valid40 in each new dataframe:

e <- sample(x=nrow(valid40),size=nrow(valid40)*0.50,)

train20 <-valid40[e,]
valid20 <- valid40[-e,]
like image 23
Jeppe Olsen Avatar answered Mar 30 '23 23:03

Jeppe Olsen