1

I have a large dataframe, I'd like to split this into multiple smaller data frames of equal parts.

A sample df:

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

I have the code that splits them into equal parts (let's say 10 dataframes of 10 rows each)

x=split(df, (seq(nrow(df))-1) %/% 10) 

and stores them in a list x, but I can't seem to figure out how to convert each part of x to a separate dataframe.

I tried to use lapply but my method didn't work out the way I wanted to

Any ideas?

Jaap
  • 81,064
  • 34
  • 182
  • 193
Varun
  • 1,211
  • 1
  • 14
  • 31

2 Answers2

2

You want list2env:

list2env(setNames(x,paste0("df",1:10)),environment())
# df3
# x         y
# 21 21 0.4935413
# 22 22 0.1862176
# 23 23 0.8273733
# 24 24 0.6684667
# 25 25 0.7942399
# 26 26 0.1079436
# 27 27 0.7237109
# 28 28 0.4112744
# 29 29 0.8209463
# 30 30 0.6470602
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
1

Relying on more basic functions you could do a simple loop:

for (i in seq(from = 0, to = 90, by = 10)) {
  assign(
    paste0("df", i/10), 
    df[(1 + i):(10 + i), ]
  )
}

At least for me this would be something I would trust while I would want to read the documentation of any "new" function I use.

s_baldur
  • 29,441
  • 4
  • 36
  • 69