Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split dataframe by row number in R

Tags:

split

r

This is probably really simple, but I can't find a solution:

df <- data.frame(replicate(10,sample(0:1,10,rep=TRUE)))

v <- c(3, 7)

is there an elegant way to split this dataframe in three elements (of a list) at the row number specified in v?

like image 680
Lukas Avatar asked Nov 25 '14 16:11

Lukas


People also ask

How do you split data frames by rows?

We can use the iloc() function to slice DataFrames into smaller DataFrames. The iloc() function allows us to access elements based on the index of rows and columns. Using this function, we can split a DataFrame based on rows or columns.

How do I extract row numbers in R?

The nrow() function in R programming R provides us nrow() function to get the rows for an object. That is, with nrow() function, we can easily detect and extract the number of rows present in an object that can be matrix, data frame or even a dataset.

How do you split data frames in R?

Use the split() function in R to split a vector or data frame. Use the unsplit() method to retrieve the split vector or data frame.


2 Answers

Assuming that rows 1&2 goes in the first split, 3,4,5,6 in the second and 7 to nrow(df) goes in the last

 split(df, cumsum(1:nrow(df) %in% v))

but if 1:3 rows are in the first split, then comes 4:7, and in the third 8 to nrow(df)

  split(df, cumsum(c(TRUE,(1:nrow(df) %in% v)[-nrow(df)])) )

Or as @James mentioned in the comments,

  split(df, cumsum(1:nrow(df) %in% (v+1)))
like image 115
akrun Avatar answered Oct 07 '22 10:10

akrun


Another way:

split(df, findInterval(1:nrow(df), v))

For the alternative interpretation, you can use:

split(df, cut(1:nrow(df), unique(c(1, v, nrow(df))), include.lowest=TRUE))
like image 25
Matthew Plourde Avatar answered Oct 07 '22 09:10

Matthew Plourde