Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a data frame into equal parts

Tags:

I have an example data frame:

df <- data.frame(x = 1:112, y = runif(112)) 

Is there a way to print a list of data frames with the first part of the list containing rows 1:10, the second 11:20, etc. up until the end (111:112)?

like image 657
ChrisYee90 Avatar asked May 10 '16 18:05

ChrisYee90


People also ask

How do you divide a DataFrame into 10 equal parts?

You could use split() , with rep() to create the groupings. How will I write a code such that it iteratively saves each of the 10 chunks as a csv file each with a unique filename? The x and each arguments are flippled if the goal is to split the df into n parts.

How do you divide data frames?

DataFrame elements can be divided by a pandas series or by a Python sequence as well. Calling div() on a DataFrame instance is equivalent to invoking the division operator (/). The div() method provides the fill_value parameter which is used for replacing the np.

How do you split a data frame in half?

In the above example, the data frame 'df' is split into 2 parts 'df1' and 'df2' on the basis of values of column 'Weight'. Method 2: Using Dataframe. groupby(). This method is used to split the data into groups based on some criteria.

How do you split a data frame in two variables?

You can also do the following: split(x = df, f = ~ var1 + var2...) This way, you can also achieve the same split dataframe by many variables without using a list in the f parameter.


2 Answers

You could use split(), with rep() to create the groupings.

n <- 10 nr <- nrow(df) split(df, rep(1:ceiling(nr/n), each=n, length.out=nr)) 
like image 175
Rich Scriven Avatar answered Oct 02 '22 19:10

Rich Scriven


This can be solved with nesting using tidyr/dplyr

require(dplyr)  require(tidyr)  num_groups = 10  iris %>%     group_by((row_number()-1) %/% (n()/num_groups)) %>%    nest %>% pull(data) 
like image 42
Holger Brandl Avatar answered Oct 02 '22 20:10

Holger Brandl