Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break dataframe into smaller dataframe's and save them

Tags:

dataframe

r

Need help to split one dataframe dynamically into multiple smaller dataframe’s based on a column interval and save them as well. Example:

x = data.frame(num = 1:26, let = letters, LET = LETTERS)

The above dataframe x needs to split into smaller dataframes based on value in num, in an interval of 5. The result would be 6 dataframes

> 1.    0 – 5
> 2.    6 – 10
> 3.    11 – 15
> 4.    16 -20
> 5.    21 -25
> 6.    26 – 30
like image 281
Vikram Avatar asked Apr 09 '26 03:04

Vikram


1 Answers

You can use the split function and cut function to perform the operation:

x = data.frame(num = 1:26, let = letters, LET = LETTERS)

answer<-split(x, cut(x$num, breaks=c(0, 5, 10, 15, 20, 25, 30)))

you can then pass this list to lapply for further processing.

like image 102
Dave2e Avatar answered Apr 10 '26 18:04

Dave2e



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!