Nick
Nick

Reputation: 45

How to split data into two parts of specified ratio NOT randomly

If I had a data frame with 100 rows, and I wanted to split the data into test and train data with a split ratio of 0.7, how would I split that but not randomly? For example, I want the first 70% to be one chunk and the last 30% to be another chunk. Chunk 1:

row     example
1       a
2       b
.
.
.
68      a3
69      a4
70      a5

Chunk 2:

row     example
71       a6
72       a7
.
.
.
98      b1
99      b2
100     b3

I wouldnt want random rows in each split

Upvotes: 0

Views: 244

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 101099

Another base R option, using split + findInterval

res <- split(df,findInterval(seq(nrow(df)),round(nrow(df)*0.7),rightmost.closed = T))

Upvotes: 0

tmfmnk
tmfmnk

Reputation: 39858

You can try:

split(mtcars, 1:nrow(mtcars)/nrow(mtcars) >= 0.7)

Resulting in 22 and 10 rows, respectively.

Upvotes: 2

Related Questions