Riham Nour
Riham Nour

Reputation: 395

how to split a dataframe with equal row numbers without randomization?

I have a data frame that has 182 elements and I want to split it into 26 parts with 7 elements each, but in the same order as the original data frame. I saw the split() function, but I read that it splits randomly and I want each 7 elements in sequence to be split. What function can I use?

Upvotes: 0

Views: 414

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 145775

Where did you read that split is random? That is not true.

The documentation is pretty clear at ?split...

split(x, f, drop = FALSE, ...)

split divides the data in the vector x into the groups defined by f

...

x vector or data frame containing values to be divided into groups.

f a ‘factor’ in the sense that as.factor(f) defines the grouping, or a list of such factors in which case their interaction is used for the grouping.

...

The split is based on the second argument, f. The split is as random as f is - you can choose a random f or whatever non-random f you would like. In this case, "I want to split it into 26 parts with 7 elements each", we can make a good f use rep:

split(your_data, f = rep(1:26, each = 7))

Upvotes: 3

Related Questions