Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Splitting a Dataset into Uneven Portions

I have this data set:

var_1 = rnorm(1027,1000,1000)
var_2 = rnorm(1027,1000,1000)
var_3 = rnorm(1027,1000,1000)

sample_data = data.frame(var_1, var_2, var_3)

I want to split this data into sections of 100:

list_of_dfs <- split(
  sample_data, (seq(nrow(sample_data))-1) %/% 100
)

However, since the number of rows in this data set is not cleanly divisible by 100 – I get 10 sections instead of 11 sections (i.e. 10 full sections and 1 non-full section):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

summary(list_of_dfs)
   Length Class      Mode
0  3      data.frame list
1  3      data.frame list
2  3      data.frame list
3  3      data.frame list
4  3      data.frame list
5  3      data.frame list
6  3      data.frame list
7  3      data.frame list
8  3      data.frame list
9  3      data.frame list
10 3      data.frame list
  • Is it possible to adjust the R code so that 11 sections are created instead of 10 sections?

Thank you!

>Solution :

grp_size <- 100
n <- nrow(sample_data)
split(sample_data, gl(ceiling(n/grp_size), grp_size, length = n))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading